我在我的应用中使用ViewPager
,在此寻呼机内部GridView
包含数据。用户可以从网格视图中选择一个项目,并在ViewPager
下显示详细信息。当用户点击GridView
中的项目时,该项目将按所选颜色标记。
问题是,在寻呼机的所有页面中只能选择一个项目。因此,当用户点击第1页上标记的项目时,以及当他在页面2上选择另一个项目时,它也会被标记,并且前一页面上的第一个仍然标记为已选中。这就是问题所在。我需要一次只选择一个项目,所以我需要在新选择后取消选择之前的选择。 如果它只在同一页面上,则它可以正常工作。下次选择后,取消选择上一个项目。
MainFragment -> PagerAdapter -> GridFragment -> GridViewAdapter
在GridFragment
我处理OnItemClick
并通知GridAdapter
选择了哪个位置和notifyDataSetChanged()
。
对于每个页面我都使用自己的GridView
PagerAdapter:
@Override
public Fragment getItem(int position) {
return GridFragment.getInstance(position-(Integer.MAX_VALUE / 2));
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
GridFragment:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_calednar, container, false);
mAdapter = new CalendarGridViewAdapter(getActivity(), getDateTime(position), MONDAY);
mGridView = (GridView) rootView.findViewById(R.id.gridView);
mGridView.setAdapter(mAdapter);
// TODO handle on item click listener
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
mAdapter.setSelectedItem(i);
mAdapter.notifyDataSetChanged();
}
});
return rootView;
}
GridViewAdapter:
@Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
// if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.grid_item_day, null);
//}
TextView txt = (TextView) convertView.findViewById(R.id.dayText);
txt.setText(""+datetimeList.get(i).getDay());
View actSelection = convertView.findViewById(R.id.actSelection);
actSelection.setVisibility(View.INVISIBLE);
if(selectedPosition == i){
actSelection.setVisibility(View.VISIBLE);
}
....
return convertView;
}
public void setSelectedItem(int position){
selectedPosition = position;
}
答案 0 :(得分:1)
您需要将所选项目索引保留在寻呼机适配器内。点击片段内的任何项目都会更改该值。
interface ItemSelectionInterface{
void onItemSelectionChanged(int fragmentPosition,int itemIndex);
int getSelectedItemOnFragment(int fragmentPosition);
}
在PagerAdapter中实现上面的接口:
class YourPagerAdapter....... implements ItemSelectionInterface{
int selectedFragment,selectedItem;
@Override
public Fragment getItem(int position) {
return GridFragment.getInstance(position-(Integer.MAX_VALUE / 2),this);
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
void onItemSelectionChanged(int fragmentPosition,int itemIndex){
selectedFragment=fragmentPosition;selectedItem=itemIndex;
}
int getSelectedItemOnFragment(int fragmentPosition){
if(fragmentPosition!=selectedFragment) return -1;
return selectedItem;
}
}
将GridFragment更改为:
class GridFragment ....{
ItemSelectionInterface selectionInterface;
@Override
public void setMenuVisibility(final boolean visible) {
super.setMenuVisibility(visible);
if (visible) {
mAdapter.notifyDataSetChanged();
}
}
public static GridFragment getInstance(int position, ItemSelectionInterface selectionInterface){
...........
...........
this.selectionInterface=selectionInterface;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_calednar, container, false);
mAdapter = new CalendarGridViewAdapter(getActivity(), getDateTime(position), MONDAY);
mAdapter.setSelectionInterface(selectionInterface);
mGridView = (GridView) rootView.findViewById(R.id.gridView);
mGridView.setAdapter(mAdapter);
// TODO handle on item click listener
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
selectionInterface.onItemSelectionChanged(position,i);
mAdapter.notifyDataSetChanged();
}
});
}
return rootView;
}
最后在适配器中:
ItemSelectionInterface selectionInterface; //Create a setter for
int position;//create a setter
@Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
// if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.grid_item_day, null);
//}
TextView txt = (TextView) convertView.findViewById(R.id.dayText);
txt.setText(""+datetimeList.get(i).getDay());
View actSelection = convertView.findViewById(R.id.actSelection);
actSelection.setVisibility(View.INVISIBLE);
if(selectionInterface.getSelectedItemOnFragment(position)== i){
actSelection.setVisibility(View.VISIBLE);
}
....
return convertView;
}