Android - 选择列表或网格的可能性

时间:2015-03-27 18:43:31

标签: android listview gridview adapter

当我在标题中写道时,我想研究一下你可以选择ListView或GridView。 我用谷歌搜索,但我找不到足够的。

在我的情况下,我有一个自定义ListView的适配器,如果我想实现一个GridView,你是怎么做到的?

我感谢任何资源,如果您了解教程!谢谢:))

2 个答案:

答案 0 :(得分:0)

要在没有大量重复逻辑的情况下实现这些不同的视图,您应该考虑使用 RecyclerView 并动态地将其 LayoutManager 更改为您的用户想要的内容。

答案 1 :(得分:0)

我为GridView和ListView使用了相同的自定义适配器。 CustomAdapter:

package com.example.nadiaakter.gridviewexample;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class CustomAdapter extends BaseAdapter {
    ImageView imageView;
    Context mContext;

    public CustomAdapter(Context mContext){
        this.mContext = mContext;
    }
    @Override
    public int getCount() {
        return images.length;
    }

    @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) {
        if (convertView == null){
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8,8,8,8);
        }else {
            imageView = (ImageView) convertView;
        }
        imageView.setImageResource(images[position]);
        return imageView;
    }
    Integer[] images = {
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7
    };
}

activity_main.xml中

android:id="@+id/gvImage"
android:columnWidth="90dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:fadingEdge="horizontal"
android:padding="10dp"

/>

MainActivity.java

package com.example.nadiaakter.gridviewexample;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.GridView;


public class MainActivity extends ActionBarActivity {
    GridView gridView;
    CustomAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gridView = (GridView) findViewById(R.id.gvImage);
        adapter = new CustomAdapter(this);
        gridView.setAdapter(adapter);

    }   

}

注意:这是GridView示例。 ListView示例(相同的CustomAdapter)如下所示:

XML

<ListView  xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/lvImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fadingEdge="horizontal"
android:padding="10dp"
    />

再次使用MainActivity.java

package com.example.nadiaakter.gridviewexample;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.ListView;


public class MainActivity extends ActionBarActivity {
    ListView listView;
    CustomAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.lvImage);
        adapter = new CustomAdapter(this);
        listView.setAdapter(adapter);

    }

}