Android适配器及适配器控件

Android适配器及适配器控件

ArrayAdapter

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_studentlist"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.mystudy.StudentlistActivity">
<ListView
    android:id="@+id/student_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></ListView>
</RelativeLayout>



public class StudentlistActivity extends AppCompatActivity {
private ListView student_name;
    private String[] name={"你要","乖乖","听话"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_studentlist);
        student_name= (ListView) findViewById(R.id.student_name);
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,name);
        student_name.setAdapter(adapter);
        student_name.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String namelist=name[position];
                Intent intent=new Intent(StudentlistActivity.this,MainActivity.class);
                intent.putExtra("NAME",namelist);
                startActivity(intent);
            }
        });
    }
}

自定义适配器

命名 extends BaseAdapter

public class ListstudentAdapter extends BaseAdapter {
    private Context context;
    private List<Studentname> studentnameList;

    public ListstudentAdapter(Context context, List<Studentname> studentnameList) {
        this.context = context;
        this.studentnameList = studentnameList;
    }

    @Override
    public int getCount() {
        return studentnameList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view= LayoutInflater.from(context).inflate(R.layout.student_item,null);
        TextView nametv= (TextView) view.findViewById(R.id.student_itemname);
        TextView namecity= (TextView) view.findViewById(R.id.student_itemcity);
        TextView nameage= (TextView) view.findViewById(R.id.student_itemage);

        nametv.setText(studentnameList.get(position).getName());
        namecity.setText(studentnameList.get(position).getCity());
        nameage.setText(studentnameList.get(position).getAge()+"");
        return view;
    }
}

SimpleAdapter

public class MainActivity extends AppCompatActivity {  

    //用三个数组装载数据  
    private String[] names = new String[]{"大雄", "叶子", "吴叶"};  
    private String[] says = new String[]{"无敌学渣", "无敌美女", "无敌可爱"};  
    private int[] imgIds = new int[]{R.mipmap.icon1, R.mipmap.icon2, R.mipmap.icon3};  

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

        List<Map<String, Object>> listitem = new ArrayList<Map<String, Object>>();  
        for (int i = 0; i < names.length; i++) {  
            Map<String, Object> showitem = new HashMap<String, Object>();  
            showitem.put("touxiang", imgIds[i]);  
            showitem.put("name", names[i]);  
            showitem.put("says", says[i]);  
            listitem.add(showitem);  
        }  
        //创建一个simpleAdapter  
        SimpleAdapter myAdapter = new SimpleAdapter(getApplicationContext(),listitem,  
                R.layout.list_item,new String[]{"touxiang","name","says"},  
                new int[]{R.id.imgtou,R.id.name,R.id.says});  
          ListView listView = (ListView)findViewById(R.id.list_test);  
          listView.setAdapter(myAdapter);  
    }  
}  

simpleadapter和arrayadapter的区别:

Simpleadapter可以展示混合数据值类型,而Arrayaadapter只能展示单个数据类型意味着

PageAdapter:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_viewpager0"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.mystudy.Viewpager0Activity">
<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/view_v4"></android.support.v4.view.ViewPager>
</RelativeLayout>
OneFragment
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.mystudy.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class OneFragment extends Fragment {


    public OneFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_one, container, false);
    }

}
TwoFragment
package com.example.mystudy.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.mystudy.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class TwoFragment extends Fragment {


    public TwoFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_two, container, false);
    }

}

FragmentAdapter

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.List;

/**
 * Created by 哈哈哈 on 2018/1/15.
 */

public class FragmentAdapter extends FragmentPagerAdapter {
    private List<Fragment> fragmentList;

    public FragmentAdapter(FragmentManager fm, List<Fragment> fragmentList) {
        super(fm);
        this.fragmentList = fragmentList;
    }

    @Override
    public Fragment getItem(int position) {
        return fragmentList.get(position);
    }

    @Override
    public int getCount() {
        return fragmentList.size();
    }
}

Viewpager0Activity

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.example.mystudy.alladapter.FragmentAdapter;
import com.example.mystudy.fragment.OneFragment;
import com.example.mystudy.fragment.TwoFragment;

import java.util.ArrayList;
import java.util.List;

public class Viewpager0Activity extends AppCompatActivity {
private ViewPager viewPager;
    private List<Fragment> fragmentList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewpager0);
        viewPager= (ViewPager) findViewById(R.id.view_v4);

        OneFragment oneFragment=new OneFragment();
        TwoFragment twoFragment=new TwoFragment();
        fragmentList.add(oneFragment);
        fragmentList.add(twoFragment);

        FragmentAdapter fragmentAdapter=new FragmentAdapter(getSupportFragmentManager(),fragmentList);
        viewPager.setAdapter(fragmentAdapter);
    }
}

适配器控件

ViewPager(v4包下的)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_viewpager0"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.mystudy.Viewpager0Activity">
<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/view_v4"></android.support.v4.view.ViewPager>
</RelativeLayout>

ListView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_studentlist"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.mystudy.StudentlistActivity">
<ListView
    android:id="@+id/student_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></ListView>
</RelativeLayout>

GridView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_example"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.listviewtest.ExampleActivity">
<GridView
    android:numColumns="4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/example_gridview"></GridView>
</RelativeLayout>

GridView总体代码

布局

//exampleitem:

<?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="match_parent">
<TextView
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:textSize="30sp"
    android:id="@+id/exercise_name"/>
    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:id="@+id/exercise_city"/>
    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:id="@+id/exercise_age"/>
</LinearLayout>

//GridView布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_example"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.listviewtest.ExampleActivity">
<GridView
    android:numColumns="4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/example_gridview"></GridView>
</RelativeLayout>

适配器

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.listviewtest.R;

import java.util.List;

/**
 * Created by 哈哈哈 on 2018/1/9.
 */

public class Gridexamadapter extends BaseAdapter {
private Context context;
    private List<Exam> examList;

    public Gridexamadapter(Context context, List<Exam> examList) {
        this.context = context;
        this.examList = examList;
    }

    @Override
    public int getCount() {
        return examList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view=null;
        ViewHodel viewHodel;
        if (convertView==null){
            view=LayoutInflater.from(context).inflate(R.layout.exampleitem,null);
            viewHodel=new ViewHodel();
            viewHodel.imageView= (ImageView) view.findViewById(R.id.exam_image);
            viewHodel.exam_name= (TextView) view.findViewById(R.id.exam_name);
            viewHodel.exam_number= (TextView) view.findViewById(R.id.exam_number);
            view.setTag(viewHodel);
        }else {
            view=convertView;
            viewHodel= (ViewHodel) view.getTag();
        }

        viewHodel.imageView.setImageResource(examList.get(position).getExamimage());
        viewHodel.exam_name.setText(examList.get(position).getExamname());
        viewHodel.exam_number.setText(examList.get(position).getExamnumber()+"");

        return view;
    }
    class ViewHodel{
        ImageView imageView;
        TextView exam_name,exam_number;

    }
}

实体

public class Exam {
    private int examimage;
    private int examnumber;
    private String examname;

    public Exam(int examimage, String examname, int examnumber) {
        this.examimage = examimage;
        this.examname = examname;
        this.examnumber = examnumber;
    }

    public int getExamimage() {
        return examimage;
    }

    public void setExamimage(int examimage) {
        this.examimage = examimage;
    }

    public int getExamnumber() {
        return examnumber;
    }

    public void setExamnumber(int examnumber) {
        this.examnumber = examnumber;
    }

    public String getExamname() {
        return examname;
    }

    public void setExamname(String examname) {
        this.examname = examname;
    }
}

Activity

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.widget.GridView;

import com.example.listviewtest.exercise.Exam;
import com.example.listviewtest.exercise.Gridexamadapter;

import java.util.ArrayList;
import java.util.List;

public class ExampleActivity extends AppCompatActivity {
private GridView gridView;
    private List<Exam> examList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_example);
        gridView= (GridView) findViewById(R.id.example_gridview);

        Exam exam1=new Exam(R.mipmap.ic_launcher,"宁宁",2);
        Exam exam2=new Exam(R.mipmap.ic_launcher,"宁宁",2);
        Exam exam3=new Exam(R.mipmap.ic_launcher,"宁宁",2);
        Exam exam4=new Exam(R.mipmap.ic_launcher,"宁宁",2);
        examList.add(exam1);
        examList.add(exam2);
        examList.add(exam3);
        examList.add(exam4);

        Gridexamadapter gridexamadapter=new Gridexamadapter(this,examList);
        gridView.setAdapter(gridexamadapter);

    }
}

猜你喜欢

转载自blog.csdn.net/lan266548_ning/article/details/80570225