您的位置:首页 > 移动开发 > Android开发

RecyclerView添加多种布局

2016-07-08 13:53 483 查看
上一篇文章讲述了一下ListView添加多种布局,现在RecyclerView已经火的不行不行了,那么我们再讲述一下RecyclerView添加多种布局,同样的效果图还是上篇文章中的对话列表只是ListView改为RecyclerView。

RecyclerView怎么用我就不多讲了网上很多例子,我们在Xml文件替换ListView:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<FrameLayout
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="2">
<SurfaceView
android:id="@+id/surface"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

<ImageButton
android:id="@+id/take_pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:background="@drawable/take_pic_selector" />
<ImageView
android:id="@+id/smallptoto"
android:layout_width="100dip"
android:layout_height="100dip"
/>
</FrameLayout>

<RelativeLayout
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1.0"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_above="@+id/send_ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<RelativeLayout
android:id="@+id/send_ll"
android:background="#ffffff"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
>
<EditText
android:id="@+id/edit_send"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/background_dark"
android:layout_toLeftOf="@+id/send"
android:maxLines="4"
android:scrollbars="vertical"
android:textSize="12sp"
/>
<Button android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/bg_item_select"
android:textSize="12sp"
android:textColor="@android:color/background_dark"
android:text="发送"/>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>

然后我们在Activity中同样的奖listView替换成ReceyerView并且重新定义一个ReceyerAdapter

/**
* Created by xxnan on 2016/6/30.
*/
public class MainActivityReceyerView extends AppCompatActivity implements View.OnClickListener{
private RecyclerView recycler_view;
private ReceyerAdapter myAdapter;
private EditText edit;
private Button send;
private List<TalkBean> list=new ArrayList<TalkBean>();
private int num=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.receyer_activity_main);
initView();
}
private void initView()
{
edit=(EditText)findViewById(R.id.edit_send);
send=(Button)findViewById(R.id.send);
send.setOnClickListener(this);
recycler_view=(RecyclerView) findViewById(R.id.recycler_view);
recycler_view.setLayoutManager(new LinearLayoutManager(this));
myAdapter= new ReceyerAdapter(list,getApplicationContext());
recycler_view.setAdapter(myAdapter);
}

@Override
public void onClick(View v) {
num++;
switch (v.getId())
{
case  R.id.send:
TalkBean talkBean=new TalkBean();
talkBean.talk_content=edit.getText().toString();
if(num%2==0) {
talkBean.user_no = "xxnan";
talkBean.isLeft=false;
}else
{
talkBean.user_no = "jack";
talkBean.isLeft=true;
}
list.add(talkBean);
myAdapter.setmList(list);
break;
}
//        recycler_talk_list.setSelection(list.size()-1);
edit.setText("");
}
@Override
protected void onResume() {
myAdapter.setmList(list);
super.onResume();
}

}

这里我们的ReceyerView设置LinearLayoutManager,其他基本和ListView一样,主要看一下自定义的ReceyerAdapter:

/**
* Created by xxnan on 2016/7/7.
*/
public class ReceyerAdapter extends RecyclerView.Adapter {
private List<TalkBean> mList;
private Context mContext;
ReceyerAdapter(List<TalkBean> list, Context context)
{
mList=list;
mContext=context;
}
public void setmList(List<TalkBean> list)
{
mList=list;
notifyDataSetChanged();
}
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        int type=getItemViewType(viewType);
MyHolder myHolder=null;
if(viewType==0)
myHolder=new LeftMyHolder(LayoutInflater.from(mContext).inflate(R.layout.talk_left_item,null));
else if(viewType==1)
myHolder=new RightMyHolder(LayoutInflater.from(mContext).inflate(R.layout.talk_right_item,null));
return myHolder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if(holder instanceof LeftMyHolder)
{
((LeftMyHolder) holder).talk_data_left_tv.setText(mList.get(position).talk_content);
((LeftMyHolder) holder).talk_name_tv.setText(mList.get(position).user_no);
}else if(holder instanceof  RightMyHolder )
{
((RightMyHolder) holder).talk_data_right_tv.setText(mList.get(position).talk_content);
((RightMyHolder) holder).talk_myself_tv.setText(mList.get(position).user_no);
}
}

@Override
public int getItemViewType(int position) {
int type=0;
if(mList.get(position).isLeft)
type=0;
else
type=1;
return type;
}

@Override
public int getItemCount() {
return mList==null?0:mList.size();
}
}
class MyHolder extends RecyclerView.ViewHolder
{
public MyHolder(View itemView) {
super(itemView);
}
}
class LeftMyHolder extends MyHolder
{
TextView talk_name_tv;
Button talk_data_left_tv;
public LeftMyHolder(View itemView)
{
super(itemView);
talk_name_tv=(TextView) itemView.findViewById(R.id.talk_name_tv);
talk_data_left_tv=(Button) itemView.findViewById(R.id.talk_data_left_tv);
}
}
class RightMyHolder extends MyHolder
{
TextView talk_myself_tv;
Button talk_data_right_tv;
public RightMyHolder(View itemView)
{
super(itemView);
talk_myself_tv=(TextView) itemView.findViewById(R.id.talk_myself_tv);
talk_data_right_tv=(Button) itemView.findViewById(R.id.talk_data_right_tv);
}
}

getItemViewType返回值和onCreateViewHolder中参数的viewtype是相等的,因此可根据viewtype值来区别加载不同的布局,而ListView中的getView方法直接调用一下getItemViewType获取返回值来区别加载不同的布局。同时我们定义两个Holder,LeftHolder和RightHolder都继承于MyHolder,在onCreateViewHolder中根据viewtype来加载LeftHolder和RightHolder,其他都和ReceyerView使用一样就不过多讲述了。

源码地址:https://github.com/xxnan/LiaoTianActivity/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android RecyclerView