赞
踩
ListView与Simple Adapter
public SimpleAdapter(Context context, List<? extends Map<String, ?>> data,
@LayoutRes int resource, String[] from, @IdRes int[] to) {
mData = data;
mResource = mDropDownResource = resource;
mFrom = from;
mTo = to;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
package com.example.view; import androidx.appcompat.app.AppCompatActivity; import android.annotation.SuppressLint; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.SimpleAdapter; import java.util.AbstractList; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MainActivity extends AppCompatActivity { private final String NAME="name"; private final String SEX="sex"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // //第一部分 // String[] array = { // "北京", // "田径", // "大同", // "上海", // "天津", // "湖北" // }; // ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.item,array); // ListView listView = findViewById(R.id.listView); // listView.setAdapter(adapter); //第二部分 String[] names = {"syz","wkx"}; String[] sexs = {"nan","nv"}; List<Map<String,Object>> list = new ArrayList<Map<String,Object>>(); for(int i = 0;i<names.length;i++){ Map<String,Object> item = new HashMap<String,Object>(); item.put(NAME,names[i]); item.put(SEX,sexs[i]); list.add(item); } SimpleAdapter adapter1 = new SimpleAdapter(this,list,R.layout.item,new String[]{NAME,SEX},new int[]{R.id.textView1,R.id.textView2}); ListView listView = findViewById(R.id.listView); listView.setAdapter(adapter1); } }
三部分,找控件----》布局管理----》数据显示**
public class MyActivity extends Activity { private RecyclerView recyclerView; private RecyclerView.Adapter mAdapter; private RecyclerView.LayoutManager layoutManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_activity); recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view); // use this setting to improve performance if you know that changes // in content do not change the layout size of the RecyclerView recyclerView.setHasFixedSize(true); // use a linear layout manager layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); // specify an adapter (see also next example) mAdapter = new MyAdapter(myDataset); recyclerView.setAdapter(mAdapter); } // ... }
dependencies {
implementation 'com.android.support:recyclerview-v7:28.0.0'
}
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_marginTop="10dp"
android:text="@string/textview"
android:gravity="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
package com.example.myrecycleview; import android.view.ViewGroup; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; public class adapter extends RecyclerView.Adapter { @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { return null; } @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { } @Override public int getItemCount() { return 0; } }
package com.example.myrecycleview; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import java.util.List; import java.util.zip.Inflater; public class adapter extends RecyclerView.Adapter<adapter.myviewholder> { private List<String> list;//传值的list private Context context; private View inflater; public adapter(Context context,List<String> list) { this.list = list; this.context = context; } @Override public adapter.myviewholder onCreateViewHolder( ViewGroup parent, int viewType) { //创建ViewHolder inflater = LayoutInflater.from(context).inflate(R.layout.item,parent,false); myviewholder myviewholder = new myviewholder(inflater); return myviewholder;//RecycleView的行,item.xml文件,每一行的文件,返回值 } @Override public void onBindViewHolder(@NonNull adapter.myviewholder holder, int position) { holder.textView.setText(list.get(position));//获取位置为position的文本值,映射关系,数据绑定 } // @Override // public void onBindViewHolder( RecyclerView.ViewHolder holder, int position) { // // }//外层最大的ciewholder @Override public int getItemCount() { return list.size();//多少数据 } //写自己的myviewholder class myviewholder extends RecyclerView.ViewHolder{ TextView textView; public myviewholder(View itemView) { super(itemView); textView = itemView.findViewById(R.id.textView); } } }
package com.example.myrecycleview; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import android.content.Context; import android.os.Bundle; import java.util.AbstractList; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private List<String> mList = new ArrayList<>(); private Context context = this; private adapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RecyclerView recyclerView = findViewById(R.id.recyclerView); //配置adapter initData(); //配置LinearLayoutManager LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context); linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);//垂直 //创建adapter类 adapter = new adapter(context,mList); //配置manager,赋值 recyclerView.setAdapter(adapter); recyclerView.setLayoutManager(linearLayoutManager); } private void initData() { mList.add("亚特兰大老鹰"); mList.add("夏洛特黄蜂"); mList.add("迈阿密热火"); mList.add("奥兰多魔术"); mList.add("华盛顿奇才"); mList.add("波士顿凯尔特人"); mList.add("布鲁克林篮网"); mList.add("纽约尼克斯"); mList.add("费城76人"); mList.add("多伦多猛龙"); mList.add("芝加哥公牛"); mList.add("克里夫兰骑士"); mList.add("底特律活塞"); mList.add("印第安纳步行者"); mList.add("密尔沃基雄鹿"); mList.add("达拉斯独行侠"); mList.add("休斯顿火箭"); mList.add("孟菲斯灰熊"); mList.add("新奥尔良鹈鹕"); mList.add("圣安东尼奥马刺"); mList.add("丹佛掘金"); mList.add("明尼苏达森林狼"); mList.add("俄克拉荷马城雷霆"); mList.add("波特兰开拓者"); mList.add("犹他爵士"); mList.add("金州勇士"); mList.add("洛杉矶快船"); mList.add("洛杉矶湖人"); mList.add("菲尼克斯太阳"); mList.add("萨克拉门托国王");mList.add("亚特兰大老鹰"); mList.add("夏洛特黄蜂"); mList.add("迈阿密热火"); mList.add("奥兰多魔术"); mList.add("华盛顿奇才"); mList.add("波士顿凯尔特人"); mList.add("布鲁克林篮网"); mList.add("纽约尼克斯"); mList.add("费城76人"); mList.add("多伦多猛龙"); mList.add("芝加哥公牛"); mList.add("克里夫兰骑士"); mList.add("底特律活塞"); mList.add("印第安纳步行者"); mList.add("密尔沃基雄鹿"); mList.add("达拉斯独行侠"); mList.add("休斯顿火箭"); mList.add("孟菲斯灰熊"); mList.add("新奥尔良鹈鹕"); mList.add("圣安东尼奥马刺"); mList.add("丹佛掘金"); mList.add("明尼苏达森林狼"); mList.add("俄克拉荷马城雷霆"); mList.add("波特兰开拓者"); mList.add("犹他爵士"); mList.add("金州勇士"); mList.add("洛杉矶快船"); mList.add("洛杉矶湖人"); mList.add("菲尼克斯太阳"); mList.add("萨克拉门托国王"); } }
结果展示:
参考网址:
1、https://www.jianshu.com/p/c4cfe38a91ed
2、https://developer.android.google.cn/guide/topics/ui/layout/recyclerview#java
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。