如何在Android的ListView中膨胀GridView?

2020年4月18日 53点热度 0条评论

所以我意识到我不能将其他元素放到android gridview中,也不能将gridview放到scrollview中,所以我试图将gridview放到只有1个listview列的android listview中,看是否可以使之工作,这是成功,但是当我尝试膨胀列表中的gridview时,我不断得到一个nullpointerexception来设置实际的网格适配器,是否有解决此问题的另一种方法?

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("");

}

}

解决方案如下:

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

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

Google的Market App和Google Music App仅使用列表视图。没有滚动视图!(我检查了层次结构视图)。仅使用列表视图会有些麻烦。因为getView()会更复杂。但这当然可以按您的意愿很好地工作(我终于以这种方式成功了)。