我已经分别实现了网格视图和图库。现在我想要合作这两个。也就是说,我希望GridView的第一个元素是图库本身。有什么出路吗?
这是代码 -
public class First_grid extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener {
GridView grid;
Gallery myHorizontalListView;
MyAdapter myAdapter;
String[] name = {"Chocolate brownie", "Chicken burger", "Caramel cake", "Extra cheese pizza", "Chocolate ice-cream", "Chocolate drink"};
int[] imageId = {R.drawable.brownie, R.drawable.burger, R.drawable.caramel, R.drawable.cheese, R.drawable.choco, R.drawable.chocolate};
float width, height;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_grid);
DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics();
width = displayMetrics.widthPixels;
height = displayMetrics.heightPixels;
myHorizontalListView = (Gallery) findViewById(R.id.horizontallistview);
myAdapter = new MyAdapter(this);
myHorizontalListView.setAdapter(myAdapter);
Custon_firstgrid adapter = new Custon_firstgrid(First_grid.this, imageId, name);
grid = (GridView) findViewById(R.id.grid);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//something
}
});
}
public void onDrawerItemSelected(View view, int position) {
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_search, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_search) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onBackPressed() {
finish();
System.exit(1);
super.onBackPressed();
}
public class MyAdapter extends BaseAdapter {
Context context;
int[] itemsArray = {R.drawable.brownie, R.drawable.burger, R.drawable.caramel, R.drawable.cheese, R.drawable.choco
};
MyAdapter(Context c) {
context = c;
}
public int getCount() {
return itemsArray.length;
}
public Object getItem(int position) {
return itemsArray[position];
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = LayoutInflater
.from(parent.getContext())
.inflate(R.layout.row, null);
ImageView image = (ImageView) rowView.findViewById(R.id.itemtext);
image.setImageResource(itemsArray[position]);
return rowView;
}
}
}