如何创建如下图所示的列表?

时间:2016-06-08 06:07:54

标签: android xamarin xamarin.android

A setting picture

如何在Android中做到这一点?

在我习惯参考屏幕之前,但它不能使用每个项目的图标。

2 个答案:

答案 0 :(得分:2)

这是一个简单的Listview。请参阅以下代码,并将list_row替换为所需的列表行设计.xml。这应该有所帮助。

public class ListAdapter extends BaseAdapter {
    LayoutInflater inflater;
    View row;
    public static List<String> list = new ArrayList<String>();
    Context context;

    class ViewHolder {
        ImageView ivShare;
    }

    public ListAdapter(Context context, List<String> _list) {
        this.context = context;
        this.list = _list;
    }

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

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

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

    ViewHolder holder;

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        inflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_row, parent, false);

            holder = new ViewHolder();
            holder.ivShare = (ImageView) convertView
                    .findViewById(R.id.iv_share);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.ivShare.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        return convertView;
    }
}

答案 1 :(得分:0)

ListView使用CustomAdapter来执行此操作。

MainActivity.java

public class MainActivity extends AppCompatActivity {

ListView lv_applicationlist;
ApplicationListingAdapter adapter;
ArrayList<Integer> app_image=new ArrayList<>();
ArrayList<String> app_name=new ArrayList<>();

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

    lv_applicationlist = (ListView) findViewById(R.id.lv_applicationlist);

    app_name.add("Camera");
    app_name.add("Gallery");
    app_name.add("Send");
    app_name.add("Share");

    app_image.add(R.drawable.ic_menu_camera);
    app_image.add(R.drawable.ic_menu_gallery);
    app_image.add(R.drawable.ic_menu_send);
    app_image.add(R.drawable.ic_menu_share);

    adapter = new ApplicationListingAdapter(MainActivity.this, app_name,app_image);
    lv_applicationlist.setAdapter(adapter);

  }
}

activity_main.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="vertical">

<ListView
    android:id="@+id/lv_applicationlist"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:listSelector="@android:color/transparent" />

</LinearLayout>

ApplicationListingAdapter.java

public class ApplicationListingAdapter extends BaseAdapter {

private Activity activity;
private static LayoutInflater inflater = null;
ArrayList<String> app_name;
ArrayList<Integer> app_image;

public ApplicationListingAdapter(Activity a, ArrayList<String> application_name, ArrayList<Integer> application_image) {

    activity = a;
    app_name = application_name;
    app_image = application_image;
    inflater = (LayoutInflater)     activity.getSystemService(activity.LAYOUT_INFLATER_SERVICE);
}

public int getCount() {
    return app_name.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public static class ViewHolder {

    public TextView tv_appname;
    public ImageView iv_appicon;
    public Switch mySwitch;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    View vi = convertView;

    if (convertView == null) {
        vi = inflater.inflate(R.layout.application_list_row, parent, false);
        holder = new ViewHolder();

        holder.tv_appname = (TextView) vi.findViewById(R.id.tv_appname);
        holder.iv_appicon = (ImageView) vi.findViewById(R.id.iv_appicon);
        holder.mySwitch = (Switch) vi.findViewById(R.id.mySwitch);

        vi.setTag(holder);

    } else {
        holder = (ViewHolder) vi.getTag();
    }
    holder.tv_appname.setText(app_name.get(position));
    holder.iv_appicon.setImageResource(app_image.get(position));

    if(app_name.get(position).equals("Send"))
    {
        holder.mySwitch.setVisibility(View.VISIBLE);
    }else{
        holder.mySwitch.setVisibility(View.GONE);
    }

    return vi;
   }
}

application_list_row.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="wrap_content">

<ImageView
    android:id="@+id/iv_appicon"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_gravity="center_vertical"
    android:layout_margin="10dp"
    android:scaleType="fitXY"
    android:src="@mipmap/ic_launcher" />

<TextView
    android:id="@+id/tv_appname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_margin="10dip"
    android:layout_weight="1"
    android:text="Store App"
    android:textSize="20dp" />

  <Switch
    android:id="@+id/mySwitch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="10dp"
    android:visibility="gone"
    android:layout_gravity="center_vertical" />

</LinearLayout>

输出将如下所示:

enter image description here