我尝试在AppCompatActivity中的ViewPager中的片段中创建ListView。在AppCompatActivity中,所有视图元素都是CoordinatorLayout中的包装。因为我使用了CoordinatorLayout。我必须使用RecylerView我正在尝试遵循培训 developer.android.com,但我的应用程序在我的日志后停止了。这是myFragment中代码停止的代码。
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;
}
//...
答案 0 :(得分:10)
使用此
@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;
}
你应该致电setLayoutManager
& setAdapter
上的Recyclerview
方法(分别)。
另外,
mRecyclerView = (RecyclerView) view.findViewById(android.R.id.list);
您不应该使用android.R.id.list
,因为您没有使用ListFragment
。将其替换为id
Recyclerview
(,如XML布局)。
答案 1 :(得分:0)
你是否将android.R.id.list设置为recyclerview id?否则你应该在xml文件中为recyclerview设置一个id,如下所示:
android:id="@+id/recyclerview"
并更改此行
mRecyclerView = (RecyclerView) view.findViewById(android.R.id.list);
到
mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
答案 2 :(得分:0)
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;
}
}
将回收适配器添加到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>