所以我意识到我不能将其他元素放入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("");
}
}