我正在使用GridView
并尝试切换每个网格项中文本视图的可见性。我正在使用自定义适配器并已实现相关代码并更改了android:visibility
。
我在这里缺少什么?任何帮助将不胜感激。 :)
以下是我的代码的相关部分:
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 (网格视图)
<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>
答案 0 :(得分:1)
在您的xml集中,可点击为真
<TextView
android:onClick="onClick"
android:clickable="true"
/>
使用此方法
public void toggleView(View view){
if(view.getVisibility()==View.INVISIBLE)
view.setVisibility(View.VISIBLE);
else if(view.getVisibility()==View.VISIBLE)
view.setVisibility(View.INVISIBLE);
}
并将其称为
toggleView(roleTextView);
最终产品
@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);
}
答案 1 :(得分:0)
if (R.id.tv_player_role == View.VISIBLE) {}
这是错的
你需要用isShown()
方法来做
if(roleTextView.isShown()){
//it is visible so do something
}
else {
//it is invisible
}
或使用view.getVisibility()
答案 2 :(得分:0)
34 + 1 + 1 +1 + 5 = 42
public void onClick(View v)
方法内部
View.OnClickListener
另请注意if (v.getVisibility() == View.VISIBLE)
可见性在XML中设置为roleTextView
,因此如果您不按代码切换,则无法在屏幕上点击任何内容(gone
是不够的,或者你切到很多代码)...
使用setText
获得更好的效果,如果需要,可以像其他人建议的那样实施inflater.inflate(R.layout.layout_grid_item, parent, false);
模式
答案 3 :(得分:0)
首先始终使用List和Grid的View Holder模式。它减少了滚动期间有关视图加载的大量问题。
现在针对您的问题,您必须保持隐藏文本视图的维护索引,而您还没有隐藏文本视图。因此,当网格滚动时,检查位置,然后决定天气以显示文本视图。 查看本教程以存储每行的索引(仅供参考) - http://www.androprogrammer.com/2013/10/list-view-with-check-box-using-custom.html