移动应用开发五(listview,adapter,fragment)

内容目录

    • listview的几个adapter
      • BaseAdapter
      • ArrayAdapter
      • SimpleAdapter
    • Fragment的创建和使用

listview的几个adapter

BaseAdapter

该类为抽象类,如若使用该类,需要使用其子类或者我们自定义它的子类,并重写里面的关键方法,如下示例,我们重写了getView()方法。
继承的抽象类中包括三个方法getCount(),getItem(),getItemId()

public View getView(int position, View convertView, ViewGroup parent) {
    //ViewHolder用于保存对子view的引用
    ViewHolder holder;
    if (convertView == null) {
        convertView = View.inflate(MainActivity.this,R.layout.list_item, null);
        holder = new ViewHolder();
        holder.text = (TextView) convertView.findViewById(R.id.tv_list);
        holder.icon = (ImageView) convertView.findViewById(R.id.image);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
//通过holder来绑定数据
    holder.text.setText(names[position]);
    holder.icon.setImageResource(icons[position]);
    return convertView;
}
static class ViewHolder {
    TextView text;
    ImageView icon;
}

ArrayAdapter

步骤:
声明所需要的adapter,做数据源,将数据源和列表控件进行绑定
1.用系统自带的layout绑定

public class MainActivity extends AppCompatActivity {

    ListView listView=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView=findViewById(R.id.list_array_simple1);

        //做数据源
        ArrayAdapter<ThreeDepart> arrayAdapter=new ArrayAdapter<ThreeDepart>(this,android.R.layout.simple_list_item_1);
        arrayAdapter.add(new ThreeDepart(1,"曹操","东汉"));
        arrayAdapter.add(new ThreeDepart(4,"zbq","rjxy"));
        //将数据源和列表控件进行绑定
        listView.setAdapter(arrayAdapter);
    }
}

2.自定义的layout绑定(listview单一控件)

arrayAdapter = new ArrayAdapter<ThreeDepart>(this, R.layout.list_item_array_test2);

3.自定义的layout绑定(listview中多控件,且包含控件的触发方法)

public class test4Activity extends AppCompatActivity {

    MyArrayAdapter myArrayAdapter = null;
    ListView listView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test4);
        Init();
    }

    protected void Init() {
        listView=findViewById(R.id.list_array_test4);
        //构建数据源
        myArrayAdapter = new MyArrayAdapter(test4Activity.this, R.layout.list_item_array_test3, R.id.tv_list_simple3);
        myArrayAdapter.add(new ThreeDepart(1, "张辽", "魏国"));
        myArrayAdapter.add(new ThreeDepart(2, "曹仁", "魏国"));      
        //绑定控件
        listView.setAdapter(myArrayAdapter);
    }
//自定义的MyArrayAdapter继承自ArrayAdapter
    private class MyArrayAdapter extends ArrayAdapter{

        public MyArrayAdapter(@NonNull Context context, int resource, int textViewResourceId) {
            super(context, resource, textViewResourceId);
        }

        @NonNull
        @Override
        public View getView(final int position, @NonNull View convertView, @NonNull ViewGroup parent){
            //找到数据源中每一个Item布局
            View view=super.getView(position,convertView,parent);
            //找到每一项布局中的按钮
            Button btn=view.findViewById(R.id.btn_item_simple3);
            //触发事件
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ThreeDepart threeDepart=(ThreeDepart) myArrayAdapter.getItem(position);
                    Toast.makeText(test4Activity.this,threeDepart.getTname(),Toast.LENGTH_SHORT).show();
                }
            });
            return view;
        }
    }
}

SimpleAdapter

利用from,to进行绑定,数据集用Map写

public class test5Activity extends AppCompatActivity {
    ListView listView=null;
    SimpleAdapter simpleAdapter=null;
    String[] from={"imgsrc","Infor"};
    int[] to={R.id.img_item_simple4,R.id.tv_item_simple4};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test5);
        Init();
    }
    private void Init(){
        listView=findViewById(R.id.list_simple_test5);
        //得到数据集
        simpleAdapter=new SimpleAdapter(this,getdata(),R.layout.list_item_simple_test4,from,to);
        //绑定数据源
        listView.setAdapter(simpleAdapter);
    }
    private List<Map<String,Object>> getdata(){
        List<Map<String,Object>> list=new ArrayList<>();

        Map<String,Object> map=new HashMap<>();
        map.put("imgsrc",R.drawable.cc);
        map.put("Tname","曹操");
        map.put("Infor","东汉末年朝廷大丞相,大将军,一代枭雄人物,魏武帝。");
        list.add(map);
        
        map=new HashMap<>();
        map.put("imgsrc",R.drawable.jw);
        map.put("Tname","姜维");
        map.put("Infor","诸葛亮选定的蜀汉接班人,官职为蜀汉大将军,是一个智商很高的人。");
        list.add(map0;
         
        return  list;
    }
}

Fragment的创建和使用

创建自定义类继承自Fragment
必须override两个方法:onCreateView,onViewCreated
前者用来绑定fragment的layout,后者用来写fragment中的方法和事件

public class AFragment extends Fragment {
    Button button = null;

    public AFragment() {
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_afragment, container, false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        button = view.findViewById(R.id.btn_fragment);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
      //注意,在fragment中使用,需要加上getActivity()
              Toast.makeText(getActivity(),"hahaha",Toast.LENGTH_SHORT).show();
            }
        });
    }
}

main中的切换fragment,linear1是fragment替换的布局

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                aFragment=new AFragment();
                getSupportFragmentManager().beginTransaction().add(R.id.linear1,aFragment).commitAllowingStateLoss();
            }
        });

你可能感兴趣的:(Android课程学习记录)