我正在做一個應用程序,我在滾動視圖中使用網格視圖。我已經實現了動態。,但問題是我的網格視圖在滾動視圖中顯示不正確。我想表明它全屏幕上,我do..please幫我,這裏是我的代碼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>
感謝.... :)
爲什麼要使用滾動視圖裏面你的GridView適配器,因爲GridView控件默認情況下滾動? –
@pratik是的,我知道..但在某些情況下,我有2在滾動視圖 – User11
除gridview是否有任何其他與gridview親愛的組件? –