Reputation: 57
I'm working with a GridView
and was trying to toggle the visibility of a Text View within each Grid Item. I am using a custom adapter and have implemented the relevant code and changed the android:visibility
also.
What am I missing here? Any help would be greatly appreciated. :)
Here are the relevant parts of my code:
MyAdapter.java
public class MyAdapter extends BaseAdapter {
Context context;
ArrayList<Players> playerList;
private static LayoutInflater inflater = null;
public MyAdapter(Context context, ArrayList<Players> playerList){
this.context = context;
this.playerList = playerList;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return playerList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = inflater.inflate(R.layout.layout_grid_item, null);
TextView idTextView = (TextView) convertView.findViewById(R.id.tv_player_id);
TextView nameTextView = (TextView) convertView.findViewById(R.id.tv_player_name);
TextView roleTextView = (TextView) convertView.findViewById(R.id.tv_player_role);
Players p = new Players();
p = playerList.get(position);
idTextView.setText("ID " + String.valueOf(p.getId()));
nameTextView.setText("Name " + String.valueOf(p.getName()));
roleTextView.setText("Role " + String.valueOf(p.getRole()));
toggleView(roleTextView);
return convertView;
}
public void toggleView(View view){
if(view.getVisibility() == View.INVISIBLE)
view.setVisibility(View.VISIBLE);
else if(view.getVisibility() == View.VISIBLE)
view.setVisibility(View.INVISIBLE);
}
}
layout_grid_item.xml
<TextView
android:id="@+id/tv_player_role"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:visibility="gone"
android:text="role" />
fragment_viewroles.xml (GRID VIEW)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.tuss.mafia.GameActivity" >
<GridView
android:id="@+id/gv_players"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:columnWidth="150dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center" >
</GridView>
</RelativeLayout>
Upvotes: 0
Views: 1080
Reputation: 300
In your xml set clickable to true
<TextView
android:onClick="onClick"
android:clickable="true"
/>
use this method
public void toggleView(View view){
if(view.getVisibility()==View.INVISIBLE)
view.setVisibility(View.VISIBLE);
else if(view.getVisibility()==View.VISIBLE)
view.setVisibility(View.INVISIBLE);
}
and call it like
toggleView(roleTextView);
final product
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = inflater.inflate(R.layout.layout_grid_item, null);
TextView idTextView = (TextView) convertView.findViewById(R.id.tv_player_id);
TextView nameTextView = (TextView) convertView.findViewById(R.id.tv_player_name);
TextView roleTextView = (TextView) convertView.findViewById(R.id.tv_player_role);
Players p = new Players();
p = playerList.get(position);
idTextView.setText("ID " + String.valueOf(p.getId()));
nameTextView.setText("Name " + String.valueOf(p.getName()));
roleTextView.setText("Role " + String.valueOf(p.getRole()));
//If you want to show text by default
roleTextView.setVisibility(View.VISIBLE);
//If you want to hide text by default
roleTextView.setVisibility(View.INVISIBLE);
roleTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
toggleView(view);
}
});
return convertView;
}
public void toggleView(View view){
if(view.getVisibility() == View.INVISIBLE)
view.setVisibility(View.VISIBLE);
else if(view.getVisibility() == View.VISIBLE)
view.setVisibility(View.INVISIBLE);
}
Upvotes: 1
Reputation: 19263
inside public void onClick(View v)
method of View.OnClickListener
if (v.getVisibility() == View.VISIBLE)
also note roleTextView
visibility is set in XML to gone
, so if you are not toggling it by code then there is nothing to click on screen (setText
is not sufficient, or you cutted to much code)...
use inflater.inflate(R.layout.layout_grid_item, parent, false);
for better performance and if needed implement ViewHolder
pattern as other fellas suggested
Upvotes: 0
Reputation: 6067
First of all use View Holder pattern with List and Grid always. It reduce lots of problem regarding view load during scroll.
Now for your issue you have to keep maintain index of view for which you have hidden text view and for which you have not. so when grid scroll it check for position and then decide weather to display text view or not. Check this tutorial for storing index for each row(Just a reference) - http://www.androprogrammer.com/2013/10/list-view-with-check-box-using-custom.html
Upvotes: 0
Reputation: 2123
if (R.id.tv_player_role == View.VISIBLE) {}
this wrong
you need to do it with isShown()
method
if(roleTextView.isShown()){
//it is visible so do something
}
else {
//it is invisible
}
or use view.getVisibility()
Upvotes: 0