我有一个Main活动,它包含NavigationDrawerFragment(取自android studio模板) GridViewAdapter有OnLongClickListener,它通过Main中的回调运行SettingsFragment:
主页中的:
public void ShowSettings(Drink drink, int position) {
// DialogFragment.show() will take care of adding the fragment
// in a transaction. We also want to remove any currently showing
// dialog, so make our own transaction and take care of that here.
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment prev = getFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
// Create and show the dialog.
DialogFragment settings = FragmentSettings.newInstance(drink, position);
settings.show(ft, "FragmentSettings");
GridViewAdapter:
public class GridViewAdapter extends BaseAdapter implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {
private ArrayList<Drink> listDrink = new ArrayList<Drink>();
private Activity activity;
private Context mContext;
private GridViewAdapterCallback callback;
public GridViewAdapter(Activity activity, ArrayList<Drink> listDrink) {
super();
this.listDrink = listDrink;
this.activity = activity;
}
public GridViewAdapter(Context c) {
mContext = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return listDrink.size();
}
@Override
public Drink getItem(int position) {
// TODO Auto-generated method stub
return listDrink.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
if (convertView == null) {
view = new ViewHolder();
convertView = inflator.inflate(R.layout.grid_item, null);
view.txtViewTitle = (TextView) convertView.findViewById(R.id.tvName);
view.imgViewFlag = (ImageView) convertView.findViewById(R.id.imageView1);
view.txtViewCount = (TextView) convertView.findViewById(R.id.tvCount);
convertView.setTag(view);
} else {
view = (ViewHolder) convertView.getTag();
}
view.txtViewTitle.setText(listDrink.get(position).getProductName());
switch (listDrink.get(position).getType()) {
case (1):
view.imgViewFlag.setImageResource(R.drawable.btn_beer);
break;
case (2):
view.imgViewFlag.setImageResource(R.drawable.btn_dark);
break;
default:
view.imgViewFlag.setImageResource(R.drawable.btn_shot);
break;
}
int count = callback.SqliteCountProducts(listDrink.get(position).getProductName());
view.txtViewCount.setText(String.valueOf(count));
// notifyDataSetChanged();
return convertView;
}
public void setCallback(GridViewAdapterCallback callback) {
this.callback = callback;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Log.d("My_Log", "On_click:" + position);
callback.SqliteSave(listDrink.get(position));
notifyDataSetInvalidated();
}
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
// Log.d("My_Log", "On_long_click:" + position);
callback.ShowSettings(listDrink.get(position), position);
notifyDataSetInvalidated();
return true;
}
public void UpdateGrid() {
notifyDataSetInvalidated();
}
public interface GridViewAdapterCallback {
public int SqliteCountProducts(String product);
public void SqliteSave(Drink drink);
public void ShowSettings(Drink drink, int position);
}
public static class ViewHolder {
public ImageView imgViewFlag;
public TextView txtViewTitle;
public TextView txtViewCount;
}
}
GridFragment,它包含GridViewAdapter
public class GridFragment extends Fragment {
// ArrayList<Drink> listDrink = new ArrayList<Drink>();
public GridViewAdapter mAdapter;
public GridFragment() {
// Required empty public constructor
}
public static GridFragment newInstance() {
GridFragment fragment = new GridFragment();
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_grid, container, false);
// prepared arraylist and passed it to the Adapter class
mAdapter = new GridViewAdapter(getActivity(), ((Main) getActivity()).PrepareDefaultArray());
GridView gridView = (GridView) view.findViewById(R.id.gvMain);
mAdapter.setCallback((GridViewAdapter.GridViewAdapterCallback) view.getContext());
gridView.setAdapter(mAdapter);
gridView.setOnItemClickListener(mAdapter);
gridView.setOnItemLongClickListener(mAdapter);
return view;
}
@Override
public void onResume() {
super.onResume();
UpdateArray();
}
public void UpdateArray() {
mAdapter.notifyDataSetChanged();
}
onResume可以工作,但只有在GridView项目滑入屏幕后面并返回
之后我不知道如何让它正常工作。 :/
内容提供商?
我不能再对main进行回调 - 因为它会导致循环
感谢您的帮助
答案 0 :(得分:0)
为什么要将UpdateArray()
放入onCreate()? mAdapter尚未实例化。
@覆盖 public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
UpdateArray();
}
在UpdateArray()
onCreate()
从onCreateView()
移至gridView.setAdapter(mAdapter);
的更新:强>
还评论notifyDataSetInvalidated ()
,因为:
public void notifyDataSetInvalidated(): 通知附加的观察者基础数据不再有效或可用。调用后,此适配器不再有效,不应报告进一步的数据集更改。