android gridview没有在全屏显示?

时间:2014-05-21 06:45:39

标签: android gridview

我正在制作一个应用程序,其中我在滚动视图中使用网格视图。我已经动态实现了。但问题是我的网格视图在滚动视图中没有正确显示。我想在全屏显示它,我做什么..请帮助我。,这是我的代码

  public class MainActivity extends Activity {
//  RelativeLayout rl;
GridView grid;
List<String> list;
Button button1;
ArrayAdapter<String> adp;
ImageView image_view;
boolean isList = false;
LinearLayout l1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //rl=(RelativeLayout) findViewById(R.id.rl);
    l1 = (LinearLayout)findViewById(R.id.l1);

    LayoutInflater inflater = LayoutInflater.from(MainActivity.this);



    LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.header, null, false);
    image_view = (ImageView) layout.findViewById(R.id.image_view);
    l1.addView(layout);


    grid =new GridView(MainActivity.this);
    list=new ArrayList<String> ();

    list.add("Dynamic 1");
    list.add("Dynamic 2");
    list.add("Dynamic 3");
    list.add("Dynamic 4");
    list.add("Dynamic 5");
    list.add("Dynamic 6");
    list.add("Dynamic 7");
    list.add("Dynamic 8");
    list.add("Dynamic 9");
    list.add("Dynamic 1");
    list.add("Dynamic 2");
    list.add("Dynamic 3");
    list.add("Dynamic 4");
    list.add("Dynamic 5");
    list.add("Dynamic 6");
    list.add("Dynamic 7");
    list.add("Dynamic 8");
    list.add("Dynamic 9");
    list.add("Dynamic 1");
    list.add("Dynamic 2");
    list.add("Dynamic 3");
    list.add("Dynamic 4");
    list.add("Dynamic 5");
    list.add("Dynamic 6");
    list.add("Dynamic 7");
    list.add("Dynamic 8");
    list.add("Dynamic 9");
    list.add("Dynamic 1");
    list.add("Dynamic 2");
    list.add("Dynamic 3");
    list.add("Dynamic 4");
    list.add("Dynamic 5");
    list.add("Dynamic 6");
    list.add("Dynamic 7");
    list.add("Dynamic 8");
    list.add("Dynamic 9");


    adp =new ArrayAdapter<String> (this,
            android.R.layout.simple_dropdown_item_1line,list);
    grid.setNumColumns(2);
    grid.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);

    grid.setAdapter(adp);
    //lL.addView(grid);
    isList = false;
    LinearLayout.LayoutParams lp = new   LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    grid.setLayoutParams(lp);

    l1.addView(grid);

    image_view.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if(!isList){
                isList = true;
                grid.setNumColumns(1);
            } 
            else if(isList)
            {
                isList = false;
                grid.setNumColumns(2);
            }
        }
    });

    grid.setOnItemClickListener(new OnItemClickListener() {


        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {

            Toast.makeText(getBaseContext(), list.get(arg2),
                    Toast.LENGTH_SHORT).show();
        }
    });

    /*grid.setOnTouchListener(new OnTouchListener(){
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_MOVE){
                return true;
            }
            return false;
        }

    });*/
}

@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;
}

   }

这里是适配器类

    public class healperadapter {
public static void getListViewSize(ListView myListView) {
    ListAdapter myListAdapter = myListView.getAdapter();
    if (myListAdapter == null) {
        //do nothing return null
        return;
    }
    //set listAdapter in loop for getting final size
    int totalHeight = 0;
    for (int size = 0; size < myListAdapter.getCount(); size++) {
        View listItem = myListAdapter.getView(size, null, myListView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }
    //setting listview item in adapter
    ViewGroup.LayoutParams params = myListView.getLayoutParams();
    params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
    myListView.setLayoutParams(params);
    // print height of adapter on log

}


public static void getGridViewSize(GridView grid) {
    ArrayAdapter<String> myListAdapter = (ArrayAdapter<String>)   grid.getAdapter();
    if (myListAdapter == null) {
        //do nothing return null
        return;
    }
    //set listAdapter in loop for getting final size
    int totalHeight = 0;
    for (int size = 0; size < myListAdapter.getCount(); size++) {
        View listItem = myListAdapter.getView(size, null, grid);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }
    //setting listview item in adapter
    ViewGroup.LayoutParams params = grid.getLayoutParams();
    params.height = totalHeight + (grid.getHeight() * (myListAdapter.getCount() - 1));
    grid.setLayoutParams(params);
    // print height of adapter on log

}
    }

这是xml类

<FrameLayout
    android:id="@+id/frameLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
</FrameLayout>

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/frameLayout1" >

    <LinearLayout
        android:id="@+id/l1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    </LinearLayout>
</ScrollView>

</RelativeLayout>

它是header.xml

<RelativeLayout
    android:id="@+id/category_layout"
    android:layout_width="fill_parent"
    android:layout_height="41dp"
    android:layout_marginTop="5dp" >

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_margin="3dp"
        android:background="@drawable/ic_launcher"
        android:visibility="visible" />

    <TextView
        android:id="@+id/Itemfound"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="5dp"
        android:layout_toLeftOf="@+id/view1"
        android:singleLine="true"
        android:textColor="#FFFFFF"
        android:textSize="13sp" />

    <View
        android:id="@+id/view1"
        android:layout_width="1dp"
        android:layout_height="fill_parent"
        android:layout_margin="2dp"
        android:layout_toLeftOf="@+id/image_view"
        android:background="#009900" />
</RelativeLayout>

<View
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:background="#009900" >
</View>

</LinearLayout>

提前致谢.... :)

1 个答案:

答案 0 :(得分:2)

尝试使用以下网格视图。

public class ExpandableHeightGridView extends GridView {

    boolean expanded = false;

    public ExpandableHeightGridView(Context context) {
        super(context);
    }

    public ExpandableHeightGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ExpandableHeightGridView(Context context, AttributeSet attrs,
            int defaultStyle) {
        super(context, attrs, defaultStyle);
    }

    public boolean isExpanded() {
        return expanded;
    }

    public void setExpanded(boolean expanded) {
        this.expanded = expanded;
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (isExpanded()) {
            int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
                    MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);

            ViewGroup.LayoutParams params = getLayoutParams();
            params.height = getMeasuredHeight();
        } else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
}

在gridview中设置适配器后写gridview.isExpanded(true);