Reputation: 4470
In My application i customized gridview to show header and subitems. But i can't select individual items. My codes as follows
main_activity
public class MainActivity extends Activity {
private TextView tv;
private GridView gv;
private GridAdapter ga;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.TextMsg);
gv = (GridView)findViewById(R.id.gridView);
ga = new GridAdapter(this);
gv.setAdapter(ga);
GridAdapter.Item item = new GridAdapter.Item();
item.set("Header1", "Item1_1", "Item1_2", "Item1_3");
ga.add(item);
item = new GridAdapter.Item();
item.set("Header2", "Item2_1", "Item2_2", "Item2_3");
ga.add(item);
item = new GridAdapter.Item();
item.set("Header3", "Item3_1", "Item3_2", "Item3_3");
ga.add(item);
}
}
GridAdapter.java
package com.example.testapplication;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class GridAdapter extends ArrayAdapter<GridAdapter.Item> {
private LayoutInflater m_vi = null;
private GridAdapter.Item m_item = null;
private ViewHolderItem m_holderItem = null;
public GridAdapter(Context context) {
super(context, 0);
m_vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
m_item = (GridAdapter.Item)getItem(position);
if (convertView == null || !convertView.getTag().equals(m_holderItem)){
convertView = m_vi.inflate(R.layout.mygrid, null);
m_holderItem = new ViewHolderItem();
convertView.setTag(m_holderItem);
}
else {
m_holderItem = (ViewHolderItem) convertView.getTag();
}
m_holderItem.tvHeader = (TextView)convertView.findViewById(R.id.gridhead);
m_holderItem.tvItem1 = (TextView) convertView.findViewById(R.id.gridtext1);
m_holderItem.tvItem2 = (TextView) convertView.findViewById(R.id.gridtext2);
m_holderItem.tvItem3 = (TextView) convertView.findViewById(R.id.gridtext3);
m_holderItem.tvHeader.setText(m_item.getHeader());
m_holderItem.tvItem1.setText(m_item.getItem1());
m_holderItem.tvItem2.setText(m_item.getItem2());
m_holderItem.tvItem3.setText(m_item.getItem3());
return convertView;
}
public static class ViewHolderItem {
public TextView tvHeader;
public TextView tvItem1;
public TextView tvItem2;
public TextView tvItem3;
}
public static class Item {
public String Header, Item1, Item2, Item3;
public Item(){
this.Header = "";
this.Item1 = "";
this.Item2 = "";
this.Item3 = "";
}
public void set(String Header_i, String Item1_i, String Item2_i, String Item3_i) {
this.Header = Header_i;
this.Item1 = Item1_i;
this.Item2 = Item2_i;
this.Item3 = Item3_i;
}
public String getHeader(){
return this.Header;
}
public String getItem1(){
return this.Item1;
}
public String getItem2(){
return this.Item2;
}
public String getItem3(){
return this.Item3;
}
}
}
mygrid-xml
<?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">
<TextView
android:id="@+id/gridhead"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/gridtext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<View
android:layout_width="20dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/gridtext2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<View
android:layout_width="20dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/gridtext3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<View
android:layout_width="wrap_content"
android:layout_height="5dp"
/>
</LinearLayout>
present image;
i just want o/p like just below
Select only item2-2
Upvotes: 1
Views: 272
Reputation: 10177
Since the items you want selected are not distinct gridview items, but children of gridview items, I believe your only option is to set the onClickListeners for each of the textViews you add in the getView method, and write your own code for coloring the background of the "selected" textview, returning the previous textview to normal color, etc.
It also requires a fairly good understanding of how adapter views are recycled, and such.
Edit: A rough outline how to accomplish this:
It is a bit of an undertaking. The best I can post for you is a good outline:
setOnClickListener for each View you hope to capture a click. These will be the particular TextViews you add in the GetView method.
Do whatever you want to graphically indicate the view is selected. (e.g. yellow background).
Record the position of the selected view.
Find previous selected View, and remove graphical selection, if needed.
Because views get recycled, whatever you do graphically for the "selected" view, may start appearing on other views as you scroll, if the graphical elements are not checked and removed everytime getView is called; they will also need to be reapplied when the user scrolls back.
Upvotes: 2