赞
踩
下例演示的是List View控件,还可以使用Spinner
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; public class MainActivity2 extends AppCompatActivity { private String[] positList = {"重庆","湖北","四川","贵州","新疆","甘肃","西藏","湖南"};//实体数据 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); //适配器 ArrayAdapter adapter = new ArrayAdapter<>(this,R.layout.itrm_array,positList); //传递给具体节点 ListView list1 = findViewById(R.id.list1); list1.setAdapter(adapter); } }
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">
<ListView
android:id="@+id/list1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="20dp"
android:textColor="@color/black"
android:gravity="center">
</TextView>
另外,关于条目文件,系统已提供了几种item的样式,一般直接使用系统的即可,不需要自定义了。如:
注意静态文件的文件名和路径
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.SimpleAdapter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MainActivity3 extends AppCompatActivity { //实体数据 private int[] imgs = {R.drawable.pic_1,R.drawable.pic_1,R.drawable.pic_1,R.drawable.pic_1,}; private String[] tits = {"标题1","标题2","标题3","标题4"}; private String[] strs={"文本信息1","文本信息2","文本信息3","文本信息4",}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3); //适配器对象,键名和id必须一一对应 SimpleAdapter adapter =new SimpleAdapter(this,getlist(),R.layout.item_simple, new String[]{"img","title","strs"}, new int[]{R.id.img_simple,R.id.txt_simple,R.id.txt_simple2}); //操作节点 ListView list2 = findViewById(R.id.list2); list2.setAdapter(adapter); } //适配器将数据集合 private List<? extends Map<String,?>> getlist() { ArrayList<Map<String,Object>> list = new ArrayList<Map<String, Object>>(); for (int i = 0; i < imgs.length; i++) { Map<String, Object> itme = new HashMap<>(); itme.put("img",imgs[i]); itme.put("title",tits[i]); itme.put("strs",strs[i]); list.add(itme); } return list; } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical"> <ListView android:id="@+id/list2" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="100dp" android:orientation="horizontal"> <ImageView android:id="@+id/img_simple" android:layout_width="0dp" android:layout_weight="3" android:layout_height="100dp"/> <LinearLayout android:layout_width="0dp" android:layout_weight="7" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/txt_simple" android:layout_width="match_parent" android:layout_weight="2" android:layout_height="0dp" android:textSize="10dp"/> <TextView android:id="@+id/txt_simple2" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="3" android:textSize="10dp"/> </LinearLayout> </LinearLayout>
(必须在适配器里面继承和重写BaseAdapter抽象类)
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.ListView; import com.example.myapplication.baseAdapter.MyAdapter1; import com.example.myapplication.bean.MyData; public class MainActivity4 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main4); //实例化适配器 MyAdapter1 adapter1 = new MyAdapter1(this, MyData.getList()); //操作页面节点 ListView list3 = findViewById(R.id.list3); list3.setAdapter(adapter1); } }
package com.example.myapplication.bean; import com.example.myapplication.R; import java.util.ArrayList; public class MyData { //数据实体 private static int[] imgs = {R.drawable.pic_1,R.drawable.pic_1,R.drawable.pic_1,R.drawable.pic_1,}; private static String[] tits = {"标题1","标题2","标题3","标题4"}; private static String[] strs={"文本信息1","文本信息2","文本信息3","文本信息4",}; //成员变量 public final int img; public final String tit; public final String str; //构造方法 public MyData(int img, String tit, String str) { this.img = img; this.tit = tit; this.str = str; } //得到数据 public static ArrayList<MyData> getList() { ArrayList<MyData> list = new ArrayList<>(); for (int i = 0; i < imgs.length; i++) { list.add(new MyData(imgs[i],tits[i],strs[i])); } return list; } }
package com.example.myapplication.baseAdapter; import android.annotation.SuppressLint; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.example.myapplication.R; import com.example.myapplication.bean.MyData; import java.util.ArrayList; public class MyAdapter1 extends BaseAdapter { private final Context mContext; private final ArrayList<MyData> mlist; public MyAdapter1(Context mContext, ArrayList<MyData> mlist) { this.mContext = mContext; this.mlist = mlist; } @Override public int getCount() { return mlist.size(); } @Override public Object getItem(int i) { return i; } @Override public long getItemId(int i) { return i; } @SuppressLint("ViewHolder") @Override public View getView(int i, View view, ViewGroup viewGroup) { View view1= LayoutInflater.from(mContext).inflate(R.layout.itme_base,null,false); ImageView img_base = view1.findViewById(R.id.img_base); TextView txt_base = view1.findViewById(R.id.txt_base); TextView txt_base2 = view1.findViewById(R.id.txt_base2); img_base.setImageResource(mlist.get(i).img); txt_base.setText(mlist.get(i).str); txt_base2.setText(mlist.get(i).tit); return view1; } }
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">
<ListView
android:id="@+id/list3"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
翻页适配器只在ViewPager上使用
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import androidx.viewpager.widget.ViewPager; import android.os.Bundle; import com.example.myapplication.baseAdapter.MyAdapter2; import com.example.myapplication.bean.MyData2lbt; import java.util.ArrayList; public class MainActivity5 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main5); //适配器 MyAdapter2 adapter2 = new MyAdapter2(this,MyData2lbt.getData()); //操作节点 ViewPager viewPager1 =findViewById(R.id.viewPager1); viewPager1.setAdapter(adapter2); } }
package com.example.myapplication.bean; import com.example.myapplication.R; import java.util.ArrayList; public class MyData2lbt { public static int[] imgs = {R.drawable.pic_1,R.drawable.pic_1,R.drawable.pic_1,R.drawable.pic_1,}; public int img; public MyData2lbt(int img) { this.img = img; } public static ArrayList<MyData2lbt> getData() { ArrayList<MyData2lbt> list = new ArrayList<>(); for (int i = 0; i < imgs.length; i++) { list.add(new MyData2lbt(imgs[i])); } return list; } }
package com.example.myapplication.baseAdapter; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import androidx.annotation.NonNull; import androidx.viewpager.widget.PagerAdapter; import com.example.myapplication.bean.MyData2lbt; import java.util.ArrayList; import java.util.List; public class MyAdapter2 extends PagerAdapter { Context mContext; ArrayList<MyData2lbt> mlist; List<ImageView> imgs = new ArrayList<>();//保存图片的数据 public MyAdapter2(Context mContext, ArrayList<MyData2lbt> mlist) { this.mContext = mContext; this.mlist = mlist; for (MyData2lbt i:mlist){ ImageView item = new ImageView(mContext); //创建条目模板 item.setLayoutParams(new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); item.setImageResource(i.img); imgs.add(item); } } @Override public int getCount() { return mlist.size(); } @Override public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { return view == object; } @NonNull @Override public Object instantiateItem(@NonNull ViewGroup container, int position) { container.addView(imgs.get(position)); return imgs.get(position); } @Override public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) { container.removeView(imgs.get(position)); } }
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
条目:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="100dp" android:layout_margin="20dp"> <ImageView android:id="@+id/img_fragmeet1" android:layout_width="0dp" android:layout_weight="2" android:layout_height="100dp"/> <LinearLayout android:layout_width="0dp" android:layout_height="100dp" android:orientation="vertical" android:layout_weight="3"> <TextView android:id="@+id/txt_fragmeet1" android:layout_width="match_parent" android:layout_weight="2" android:layout_height="0dp"/> <TextView android:id="@+id/txt_fragmeet2" android:layout_weight="3" android:layout_width="match_parent" android:layout_height="0dp" /> </LinearLayout> </LinearLayout>
适配器:
package com.example.myapplication.fragmeet; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import com.example.myapplication.R; public class fragmeet extends Fragment { Context mContext; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { mContext = getActivity();//获取活动页面的上下文 //得到条目和设置条目上标签的属性 View view = LayoutInflater.from(mContext).inflate(R.layout.item_fragmeet,null,false); ImageView img_fragmeet1 = view.findViewById(R.id.img_fragmeet1); TextView txt_fragmeet1 = view.findViewById(R.id.txt_fragmeet1); TextView txt_fragmeet2 = view.findViewById(R.id.txt_fragmeet2); img_fragmeet1.setImageResource(R.drawable.pic_1); txt_fragmeet1.setText("文本"); txt_fragmeet2.setText("文本2"); return view; } }
(在活动页面的XML中直接使用)使用:
<fragment
android:id="@+id/frag"
android:name="com.example.myapplication.fragmeet.fragmeet"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginBottom="20dp" />
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。