在开发中,有时候需要在listview的子view中用gridview来显示,这时就要解决冲突了,不过都是比较简单的,网上也有个横向的listview,不过对于手机来说还是下拉的情况比较多,
看代码
我们新建一个 InnerGridView
package com.example.listviewandgridview;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.GridView;
public class InnerGridView extends GridView {
public InnerGridView(Context context) {
super(context);
}
public InnerGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
测试activity
package com.example.listviewandgridview;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private ListView listview;
private List<ListviewData> listData=new ArrayList<ListviewData>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
for(int i=0;i<25;i++){
ListviewData data=new ListviewData();
data.setName("list"+i);
List<GridViewData> grid=new ArrayList<GridViewData>();
for(int k=0;k<10;k++){
GridViewData g=new GridViewData();
g.setName("grid"+k);
grid.add(g);
}
data.setGridData(grid);
listData.add(data);
}
// System.out.println("--------"+data);
this.listview=(ListView) findViewById(R.id.listView1);
this.listview.setAdapter(new ListViewAdapter());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
public class ListViewAdapter extends BaseAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return listData.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return listData.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ListHolder holder=null;
if(null == holder){
holder=new ListHolder();
convertView=LayoutInflater.from(MainActivity.this).inflate(R.layout.listview_item, null);
holder.name=(TextView) convertView.findViewById(R.id.textView1);
holder.gridview=(InnerGridView) convertView.findViewById(R.id.gridview);
holder.head_item=(RelativeLayout) convertView.findViewById(R.id.head_item);
convertView.setTag(holder);
}else{
holder=(ListHolder) convertView.getTag();
}
holder.name.setText(listData.get(position).getName());
System.out.println("-----gridview-------"+holder.gridview);
System.out.println("---------getGridData-------"+listData.get(position).getGridData());
holder.gridview.setAdapter(new GridViewAdapter(listData.get(position).getGridData()));
holder.head_item.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, ""+position, Toast.LENGTH_LONG).show();
}
});
return convertView;
}
}
public class GridViewAdapter extends BaseAdapter{
private List<GridViewData> list;
public GridViewAdapter(List<GridViewData> data) {
// TODO Auto-generated constructor stub
list=data;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
GridHolder holder=null;
if(null == convertView){
holder=new GridHolder();
convertView=LayoutInflater.from(MainActivity.this).inflate(R.layout.gridview_item, null);
holder.name=(TextView) convertView.findViewById(R.id.textView1);
holder.child_item=(RelativeLayout) convertView.findViewById(R.id.child_item);
convertView.setTag(holder);
}else{
holder=(GridHolder) convertView.getTag();
}
holder.name.setText(list.get(position).getName());
holder.child_item.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, ""+position, Toast.LENGTH_LONG).show();
}
});
return convertView;
}
}
public static class ListHolder{
TextView name;
InnerGridView gridview;
RelativeLayout head_item;
}
public static class GridHolder{
TextView name;
RelativeLayout child_item;
}
}
看看head_item
<?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" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:id="@+id/head_item">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@+id/imageView1"
android:text="TextView" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<com.example.listviewandgridview.InnerGridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="4dip"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="4dip" ></com.example.listviewandgridview.InnerGridView>
</LinearLayout>
</LinearLayout>
差不多了哦。看看效果吧