1.ListView定制化使用 -重写适配器、自定义列表项布局
(1)定义一个类 封装每个列表项数据
public class Fruit {
private String name;
private int imageId;
public Fruit(String name, int imageId) {
this.name = name;
this.imageId = imageId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
}
2.自定义列表项的布局文件 fruit_item.xml
<?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="match_parent"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/fruit_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dip" />
</LinearLayout>
3.主界面-ListView
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv1"/>
</androidx.constraintlayout.widget.ConstraintLayout>
4.定义适配器:FruitAdapter,这个适配器继承自 ArrayAdapter。重写构造方法和 getView 方法。
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.List;
public class FruitAdapter extends ArrayAdapter {
private final int resourceId;
public FruitAdapter(@NonNull Context context, int resource, @NonNull List<Fruit> objects) {
super(context, resource, objects);
resourceId=resource;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Fruit fruit = (Fruit) getItem(position); // 获取当前项的Fruit实例
View view = LayoutInflater.from(getContext()).inflate(resourceId, null);//实例化一个对象
//实现数据跟控件一一对应
ImageView fruitImage = (ImageView) view.findViewById(R.id.fruit_image);//获取该布局内的图片视图
TextView fruitName = (TextView) view.findViewById(R.id.fruit_name);//获取该布局内的文本视图
fruitImage.setImageResource(fruit.getImageId());//为图片视图设置图片资源
fruitName.setText(fruit.getName());//为文本视图设置文本内容
return view;
}
}
5.将数据源适配到控件上
1.创建主界面
<?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="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button2" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
2.创建碎片Fragmet New -Fragment
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
* Use the {@link Fragment1#newInstance} factory method to
* create an instance of this fragment.
*/
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_1, container, false);
}
}
3.实现将碎片Fragment添加到Activity
manager=getFragmentManager();//1.创建碎片管理器
transaction=manager.beginTransaction();//2.启动事务管理
//指定fragment的id的布局 然后给他添加控件 fragment2.class也一样 这是第一次加载 所以下面是transaction.add()的方法
transaction.add(R.id.layout,new Fragment1());//3.事务中实现将碎片添加到Activity
transaction.commit();//4提交事务。
import androidx.appcompat.app.AppCompatActivity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//private List<Fruit> fruitList = new ArrayList<Fruit>();
Button bt1;
Button bt2;
private FragmentManager manager;
private FragmentTransaction transaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlayout);
bt1=(Button)findViewById(R.id.button1);
bt2=(Button)findViewById(R.id.button2);
bt1.setOnClickListener(this);
bt2.setOnClickListener(this);
manager=getFragmentManager();
transaction=manager.beginTransaction();
//指定fragment的id的布局 然后给他添加控件 fragment2.class也一样 这是第一次加载 所以下面是transaction.add()的方法
transaction.add(R.id.layout,new Fragment1());
transaction.commit();
/*setContentView(R.layout.activity_main);
initFruits(); // 初始化水果数据
FruitAdapter adapter = new FruitAdapter(MainActivity.this, R.layout.fruit_item, fruitList);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);*/
}
@Override
public void onClick(View v) {
transaction=manager.beginTransaction();
switch (v.getId()){
case R.id.button1:
//在这里是从新加载 所以就要用到transaction.replace()的了
transaction.replace(R.id.layout,new Fragment1());
break;
case R.id.button2:
transaction.replace(R.id.layout,new Fragment2());
break;
}
transaction.commit();
}
/*private void initFruits() {
Fruit apple = new Fruit ("APPLE",R.mipmap.ic_launcher,20);
fruitList.add(apple);
Fruit banana = new Fruit("BBB",R.mipmap.ic_launcher,20);
fruitList.add(banana);
}*/
}