Java 在片段中实现RecyclerView
我试图在AppActivity中的ViewPager内的片段中创建ListView。在AppCompatitorActivity中,所有视图元素都以协调布局方式展开。因为我用了坐标仪。我必须使用RecylerView,我正试图从中了解培训内容 ,但我的应用程序在登录后停止。这是我在myFragment中的代码,应用程序在其中停止Java 在片段中实现RecyclerView,java,android,android-fragments,android-recyclerview,Java,Android,Android Fragments,Android Recyclerview,我试图在AppActivity中的ViewPager内的片段中创建ListView。在AppCompatitorActivity中,所有视图元素都以协调布局方式展开。因为我用了坐标仪。我必须使用RecylerView,我正试图从中了解培训内容 ,但我的应用程序在登录后停止。这是我在myFragment中的代码,应用程序在其中停止 import android.app.Activity; import android.os.Bundle; import android.support.v4.app
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
//...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_city_list, container, false)
mRecyclerView = (RecyclerView) view.findViewById(android.R.id.list);
mLayoutManager = new LinearLayoutManager(this.getActivity());
Log.d("debugMode", "The application stopped after this");
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new RecyclerAdapter(getNames());
mRecyclerView.setAdapter(mAdapter);
return view;
}
//...
用这个
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_city_list, container, false)
// Replace 'android.R.id.list' with the 'id' of your RecyclerView
mRecyclerView = (RecyclerView) view.findViewById(android.R.id.list);
mLayoutManager = new LinearLayoutManager(this.getActivity());
Log.d("debugMode", "The application stopped after this");
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new RecyclerAdapter(getNames());
mRecyclerView.setAdapter(mAdapter);
return view;
}
您应该在Recyclerview
上调用setLayoutManager
和setAdapter
方法()
加上
mRecyclerView=(RecyclerView)view.findviewbyd(android.R.id.list)代码>
您不应该使用android.R.id.list
,因为您没有使用ListFragment
。将其替换为您的id
Recyclerview
(如在XML布局中)。您是否将android.R.id.list设置为Recyclerview id?否则,应在xml文件中为recyclerview设置一个id,如下所示:
android:id="@+id/recyclerview"
换一条线
mRecyclerView = (RecyclerView) view.findViewById(android.R.id.list);
到
公共类HomeFragment扩展了片段{
私有HomeViewModel HomeViewModel;
公共视图onCreateView(@NonNull layoutiner充气机,
视图组容器,捆绑包savedInstanceState){
homeViewModel=
ViewModelProviders.of(this.get)(HomeViewModel.class);
视图根=充气机。充气(R.layout.fragment\u home,container,false);
final TextView TextView=root.findviewbyd(R.id.text\u home);
homeViewModel.getText().observe(这是新的观察者(){
@凌驾
公共void onChanged(@Nullable String s){
textView.setText;
}
});
FragmentActivity c=getActivity();
RecyclerView RecyclerView=root.findViewById(R.id.my\u recycler\u视图);
setLayoutManager(新的GridLayoutManager(c,2));
List villageModels=new ArrayList();
VillageModel VillageModel=新的VillageModel(“,0,0,0,0,0);
添加(villageModel);
VillageModel villageModel1=新的VillageModel(“,0,0,0,0,0);
VillageModel villageModel2=新的VillageModel(“,0,0,0,0,0);
添加(villageModel1);
添加(villageModel2);
VillageAdapter=新VillageAdapter(c,villageModels);
recyclerView.setAdapter(适配器);
返回根;
}
}
将回收适配器添加到oncreateView
片段主xml结构
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
/>
</RelativeLayout>
你能发布坠机报告吗?谢谢!已经很晚了,我没有看到R.id.list没有引用我的列表!我被困了好几个小时!我有一个问题,getNames从哪里来?getNames()
是一个返回数据列表以填充RecyclerView
public class HomeFragment extends Fragment {
private HomeViewModel homeViewModel;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
final TextView textView = root.findViewById(R.id.text_home);
homeViewModel.getText().observe(this, new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
textView.setText(s);
}
});
FragmentActivity c = getActivity();
RecyclerView recyclerView = root.findViewById(R.id.my_recycler_view);
recyclerView.setLayoutManager(new GridLayoutManager(c, 2));
List<VillageModel> villageModels=new ArrayList<>();
VillageModel villageModel =new VillageModel("",0,0,0,0,0,0);
villageModels.add(villageModel);
VillageModel villageModel1 =new VillageModel("",0,0,0,0,0,0);
VillageModel villageModel2 =new VillageModel("",0,0,0,0,0,0);
villageModels.add(villageModel1);
villageModels.add(villageModel2);
VillageAdapter adapter = new VillageAdapter(c,villageModels);
recyclerView.setAdapter(adapter);
return root;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
/>
</RelativeLayout>