recyclerview展示数据(MVP)

xiaoxiao2021-02-28  9

首先新建4个包,分别是contract,model,view,presenter

然后添加recyclerview依赖

主布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.bwei.fragment.mvpdemolx.view.MainActivity"> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/mycycler" ></android.support.v7.widget.RecyclerView> </RelativeLayout> 子布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="20dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv"/> </LinearLayout> 代码

contract包下

public class Contract { public interface IModel{ List<String> getData(); } public interface IView{ void setData(List<String> list); } public interface IPresenter{ void showData(); } } model包下

public class Model implements Contract.IModel{ List<String> list=new ArrayList<>(); @Override public List<String> getData() { for (int i=0;i<50;i++){ list.add("条目"+i); } return list; } } presenter包下

public class Persenter implements Contract.IPresenter{ private Contract.IView iView; private Model model; public Persenter(Contract.IView iView) { this.iView = iView; model=new Model(); } @Override public void showData() { List<String> list=model.getData(); iView.setData(list); } } view包下

public class MainActivity extends AppCompatActivity implements Contract.IView{ private RecyclerView recyclerView; private Adapter adapter; private Persenter persenter=new Persenter(this); List<String> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = (RecyclerView) findViewById(R.id.mycycler); persenter.showData(); } @Override public void setData(List<String> list) { recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setAdapter(new Adapter(MainActivity.this,list)); } } 适配器

public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder>{ private Context context; private List<String> list=new ArrayList<>(); public Adapter(Context context, List<String> list) { this.context = context; this.list = list; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { MyViewHolder holder = new MyViewHolder(LayoutInflater.from( context).inflate(R.layout.item, parent, false)); return holder; } @Override public void onBindViewHolder(MyViewHolder holder, int position) { holder.tv.setText(list.get(position)); } @Override public int getItemCount() { return list.size(); } class MyViewHolder extends RecyclerView.ViewHolder { TextView tv; public MyViewHolder(View view) { super(view); tv = (TextView) view.findViewById(R.id.tv); } } }

转载请注明原文地址: https://www.6miu.com/read-2000042.html

最新回复(0)