我想在片段中使用recyclerview但是我遇到了这个错误: java.lang.RuntimeException:无法启动活动ComponentInfo:android.view.InflateException:二进制XML文件行#7:android.app.ActivityThread.performLaunchActivity(ActivityThread.java)中的类android.support.v7.widget.RecyclerView错误。 2195)
主要活动:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
public void init() {
setContentView(R.layout.main);
Fragment fr;
fr = new AuctionsList();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.commit();
}
片段:
public class AuctionsList extends Fragment {
private List<Goods> goodsList = new ArrayList<>();
private RecyclerView recyclerView;
private GoodsAdapter mAdapter;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Inflate the layout for this fragment
final View rootView = inflater.inflate(
R.layout.frg_auction_list, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
mAdapter = new GoodsAdapter(goodsList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this.getActivity());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setAdapter(mAdapter);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new DividerItemDecoration(this.getActivity(), LinearLayoutManager.VERTICAL));
prepareGoodsData();
mAdapter.setOnItemClickListener(new GoodsAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Goods good = goodsList.get(position);
Toast.makeText(getActivity(), good.getTitle() + " was clicked!", Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="center_horizontal">
<Button android:id="@+id/but_terms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/main_but_terms"/>
<Button android:id="@+id/but_guide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/main_but_guide"/>
<Button android:id="@+id/but_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:text="@string/main_but_login"/>
</LinearLayout>
<fragment android:name="com.ods.activity.AuctionsList"
android:id="@+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
片段布局xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</RelativeLayout>
RecyclerView item xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:clickable="true"
android:orientation="vertical">
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_alignParentLeft="true"/>
<TextView
android:id="@+id/title"
android:text="عنوان"
android:textColor="@color/title"
android:textSize="16dp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/lastBidPrice"
android:layout_below="@id/title"
android:gravity="right"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="356500"/>
<TextView
android:id="@+id/remainingTime"
android:layout_below="@id/lastBidPrice"
android:textColor="@color/year"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_height="wrap_content"
android:text="24:15:12"/>
</RelativeLayout>
答案 0 :(得分:0)
我明白了。我应该将整个RecyclerView库(Android SDK中的整个文件夹)添加到我的模块作为库模块,然后将其作为依赖项添加到我的主项目中,并添加它的jar库。