CoordinatorLayout +RecyclerView+載入不同佈局的item
阿新 • • 發佈:2019-01-24
dependencies { compile fileTree(dir: 'libs', include: '*.jar') compile 'com.android.support:appcompat-v7:23.0.1' compile 'com.android.support:design:23.0.1' compile 'com.android.support:recyclerview-v7:23.0.1' compile 'com.android.support:cardview-v7:23.0.1' compile 'com.android.support:recyclerview-v7:23.1.1' }
2、RecyclerView 通過使用RecyclerView控制元件,我們可以在APP中建立帶有Material Design風格的複雜列表。RecyclerView控制元件和ListView的原理有很多相似的地方,都是維護少量的View來進行顯示大量的資料,不過RecyclerView控制元件比ListView更加高階並且更加靈活。當我們的資料因為使用者事件或者網路事件發生改變的時候也能很好的進行顯示。和ListView不同的是,RecyclerView不用在負責Item的顯示相關的功能,在這邊所有有關佈局,繪製,資料繫結等都被分拆成不同的類進行管理。具體有關RecyclerView的講解可以參考:
package com.dictionary.adapter; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView.ViewHolder; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import com.dictionary.entity.News; import com.example.dictionary.R; import java.util.ArrayList; import java.util.List; public class RecyclerAdapter extends RecyclerView.Adapter<ViewHolder> { public final static int TYPE_ONE = 1; public final static int TYPE_TWO = 2; public final static int TYPE_THREE = 3; private Context mContext; private List<News> newses = new ArrayList<>(); private OnItemClickListener onItemClickListener; public RecyclerAdapter(List<News> newses,Context context) { super(); this.mContext = context; this.newses = newses; } public void setOnItemClickListener(OnItemClickListener onItemClickListener) { this.onItemClickListener = onItemClickListener; } public interface OnItemClickListener { void OnItemClick(View view, int position); void OnItemLongClick(View view, int position); } public void add(News news) { this.newses.add(news); notifyDataSetChanged(); } @Override public int getItemViewType(int position) { return newses.get(position).getItemType(); } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = null; ViewHolder holder = null; switch (viewType) { case TYPE_ONE: view = LayoutInflater.from(mContext).inflate(R.layout.list_item1_mainsearch, parent, false); holder = new ViewHolderOne(view); break; case TYPE_TWO: view = LayoutInflater.from(mContext).inflate(R.layout.list_item2_mainsearch, parent, false); holder = new ViewHolderTwo(view); break; case TYPE_THREE: view = LayoutInflater.from(mContext).inflate(R.layout.list_item3_mainsearch, parent, false); holder = new ViewHolderThree(view); break; } return holder; } @Override public void onBindViewHolder(final ViewHolder holder, int position) { switch (getItemViewType(position)) { case TYPE_ONE: final ViewHolderOne holderOne = (ViewHolderOne) holder; onItemEventClick(holderOne); break; case TYPE_TWO: ViewHolderTwo holderTwo = (ViewHolderTwo) holder; onItemEventClick(holderTwo); break; case TYPE_THREE: ViewHolderThree holderThree = (ViewHolderThree) holder; onItemEventClick(holderThree); } } @Override public int getItemCount() { return newses.size(); } class ViewHolderOne extends ViewHolder { TextView tv_item1; public ViewHolderOne(View itemView) { super(itemView); tv_item1 = (TextView) itemView.findViewById(R.id.tv_item1); tv_item1.setText("我是修改過得文字1"); } } class ViewHolderTwo extends ViewHolder { Button bt_item1; public ViewHolderTwo(View itemView) { super(itemView); bt_item1 = (Button) itemView.findViewById(R.id.bt_item1); } } class ViewHolderThree extends ViewHolder { TextView textView; public ViewHolderThree(View itemView) { super(itemView); textView = (TextView) itemView.findViewById(R.id.tv_content1); textView.setText("我是修改過得圖片1"); } } protected void onItemEventClick(ViewHolder holder) { final int position = holder.getLayoutPosition(); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onItemClickListener.OnItemClick(v, position); } }); holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { onItemClickListener.OnItemLongClick(v, position); return true; } }); } }
News: