0

所以我意识到我不能将其他元素放入android gridview,也不能将gridview放入scrollview,所以我尝试将gridview粘贴到只有1个listview列的android listview中,看看我是否可以做到这一点,这是成功,但是当我尝试膨胀列表内的网格视图时,我不断收到一个空指针异常,我在其中设置了实际的网格适配器,还有其他方法可以解决这个问题吗?

package com.listtest.testit;

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

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;

public class ListTestActivity extends Activity {

ViewGroup footer;
LayoutInflater inflater;
List<String> theStrings = new ArrayList<String>();
ListAdapter adapter;
ListView list;
GridView grid;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    theStrings.add("");

    String[] numbers = new String[] { 
            "A", "B", "C", "D", "E",
            "F", "G", "H", "I", "J",
            "K", "L", "M", "N", "O",
            "P", "Q", "R", "S", "T",
            "U", "V", "W", "X", "Y", "Z"};

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

    inflater = getLayoutInflater();
    footer = (ViewGroup) inflater.inflate(R.layout.footer, list, false);
    list.addFooterView(footer, null, false);

    adapter = new ListAdapter(this, theStrings);
    list.setAdapter(adapter);

    ArrayAdapter<String> gridAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, numbers);

    grid.setAdapter(gridAdapter);

    list.setOnScrollListener(new OnScrollListener() {
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // Do nothing
        }

        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {

            final int lastItem = firstVisibleItem + visibleItemCount;

            // threshold being indicator if bottom of list is hit
            if (lastItem == totalItemCount) {

                //addMoreToArray();
                //adapter.notifyDataSetChanged();
            }
        }
    });
}

public class ListAdapter extends BaseAdapter {

    private Activity activity;
    private List<String> data;
    private LayoutInflater inflater=null;


    public ListAdapter(Activity a, List<String> theStrings) {
        activity = a;
        data=theStrings;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

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

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.item, null);

        grid=(GridView)vi.findViewById(R.id.gridView1);

        return vi;
    }
}

private void addMoreToArray() {

    theStrings.add("");

}

}
4

1 回答 1

1

说你正在尝试的不是好方法,尽管他介绍了他如何在列表视图中管理网格视图。

这里是这样说的......这里的问题是,GridView 和 ListView 本身都是可滚动的,将可滚动容器嵌入到另一个可滚动容器中并不是一个好方法。


Google 的 Market App 和 Google Music App仅使用列表视图。没有滚动视图!(我检查了层次结构视图)。仅使用列表视图会有点麻烦。因为 getView() 会更复杂。但它确实如你所愿(我终于通过这种方式成功了)。

于 2014-02-13T06:46:33.037 回答