老师,我遇到了一个非常奇葩的问题,recyclerview 中的item match_parent不起作用!!!!
//由于我的AS更新到了最细版本,在新建项目的时候没有选择compile version的界面了,默认就是最新的6.0 //下面的是activity_main布局文件 <?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context=".MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_main" /> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" android:src="@android:drawable/ic_dialog_email" /> </android.support.design.widget.CoordinatorLayout> //content_main.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" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".MainActivity" tools:showIn="@layout/activity_main"> <android.support.v7.widget.RecyclerView android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="200dp"></android.support.v7.widget.RecyclerView> </RelativeLayout> //item.xml文件 <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="72dp" android:background="#44ff0000"> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" /> </FrameLayout> //MainActivity代码 package com.lee.taylor.recyclerview; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.Menu; import android.view.MenuItem; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private List<String> mDatas;//数据源 private RecyclerView mRv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); initDatas(); initView(); mRv.setAdapter(new SimpleAdapter(mDatas, this)); LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, true); mRv.setLayoutManager(manager); } private void initView() { mRv = (RecyclerView) findViewById(R.id.rv); } private void initDatas() { mDatas = new ArrayList<>(); for (int i = 'A'; i < 'z'; i++) { mDatas.add("" + (char) i); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } //SimpleAdapter package com.lee.taylor.recyclerview; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.List; /** * Created by Taylor on 2015/10/9. */ public class SimpleAdapter extends RecyclerView.Adapter<SimpleAdapter.MyViewHolder> { private List<String> mDatas; private LayoutInflater mInflater; public SimpleAdapter(List<String> mDatas, Context context) { this.mDatas = mDatas; this.mInflater = LayoutInflater.from(context); } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { /** * 此处inflate的时候 ,如果是mInflater.inflate(R.layout.listview_item, parent);会报如下错误 * * The specified child already has a parent. You must call removeView() on the child's parent first * * 它说这个特定的child已经有一个parent了,你必须在这个parent中首先调用removeView()方法,才能继续你的内容。 * * 这里很明显这个child是一个View,一个子(child)View必须依赖于父(parent)View,如果你要使用这个child, * * 则必须通过parent,而你如果就是硬想使用这个child,那么就得让这个child与parent脱离父子关系(即removeView()) */ View convertView = mInflater.inflate(R.layout.listview_item, null); MyViewHolder viewHolder = new MyViewHolder(convertView); return viewHolder; } @Override public void onBindViewHolder(MyViewHolder holder, int position) { holder.tv.setText(mDatas.get(position)); } @Override public int getItemCount() { return mDatas.size(); } /** * RecyclerView的ViewHolder */ public static class MyViewHolder extends RecyclerView.ViewHolder { TextView tv; public MyViewHolder(View itemView) { super(itemView); tv = (TextView) itemView.findViewById(R.id.tv); } } }
recyclerview中item的宽度就是没办法matchparent ,这是为什么呢?
/** *我在SimpleAdapter中重新设置了convertView的LayoutParams,item才能match_parent *修改后的SimpleAdapter */ package com.lee.taylor.recyclerview; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.List; /** * Created by Taylor on 2015/10/9. */ public class SimpleAdapter extends RecyclerView.Adapter<SimpleAdapter.MyViewHolder> { private List<String> mDatas; private LayoutInflater mInflater; public SimpleAdapter(List<String> mDatas, Context context) { this.mDatas = mDatas; this.mInflater = LayoutInflater.from(context); } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { /** * 此处inflate的时候 ,如果是mInflater.inflate(R.layout.listview_item, parent);会报如下错误 * * The specified child already has a parent. You must call removeView() on the child's parent first * * 它说这个特定的child已经有一个parent了,你必须在这个parent中首先调用removeView()方法,才能继续你的内容。 * * 这里很明显这个child是一个View,一个子(child)View必须依赖于父(parent)View,如果你要使用这个child, * * 则必须通过parent,而你如果就是硬想使用这个child,那么就得让这个child与parent脱离父子关系(即removeView()) */ View convertView = mInflater.inflate(R.layout.listview_item, null); //如果想让item match_parent 必须setLayoutParams 在布局中match_parent不起作用,暂时不知道为什么,(有可能是mInflater.inflate(R.layout.listview_item, null);的原因) convertView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 72));//加了这一句话,可以match_parent了 MyViewHolder viewHolder = new MyViewHolder(convertView); return viewHolder; } @Override public void onBindViewHolder(MyViewHolder holder, int position) { holder.tv.setText(mDatas.get(position)); } @Override public int getItemCount() { return mDatas.size(); } /** * RecyclerView的ViewHolder */ public static class MyViewHolder extends RecyclerView.ViewHolder { TextView tv; public MyViewHolder(View itemView) { super(itemView); tv = (TextView) itemView.findViewById(R.id.tv); } } }