所以我的ViewPager
上有Save to Favorite
和HomeFragment
按钮。当我点击按钮时,我想要存储图像,然后在另一个片段上检索,我使用FavGridAdapter
。我尝试使用getter / setter作为位置,但它不起作用。以下是HomeFragment
,FavGridAdapter
和FavList
(getter / setter)的代码。
HomeFragment
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v4.view.ViewPager.SimpleOnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
public class HomeFragment extends Fragment {
private static int currentPage;
ViewPager viewPager;
public static HomeFragment newInstance() {
return new HomeFragment();
}
public HomeFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container,
false);
/*
* ImageButton ib = (ImageButton) rootView.findViewById(R.id.buttonEnq);
* ib.setOnClickListener(new OnClickListener() {
*
* @Override public void onClick(View v) { InfoFragment infoFragment =
* new InfoFragment(); //FragmentManager fm = getFragmentManager();
* //fm.beginTransaction().replace(R.id.frame_container,
* infoFragment).commit();
*
* FragmentTransaction ft = getFragmentManager().beginTransaction();
* //ft.setCustomAnimations(R.anim.abc_slide_out_bottom, 0);
* ft.addToBackStack(null);
* ft.replace(R.id.frame_container,infoFragment); ft.commit(); } });
*/
viewPager = (ViewPager) rootView.findViewById(R.id.myfivepanelpager);
ImageAdapter adapter1 = new ImageAdapter(this.getActivity());
viewPager.setAdapter(adapter1);
Button buttonFav = (Button)rootView.findViewById(R.id.buttonFav);
buttonFav.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int position = viewPager.getCurrentItem();
//List<FavList> fav = new ArrayList<FavList>();
FavList item=new FavList();
item.setPosition(position);
Log.e("error", "item:" +item);
//fav.add(item);
/*CommunityFragment fragment = new CommunityFragment();
Bundle bundle = new Bundle();
bundle.putLong("position", position);
Log.e("error", "bundle:"+bundle.toString());
fragment.setArguments(bundle);*/
}
});
return rootView;
}
@Override
public void onDetach() {
super.onDetach();
try {
Field childFragmentManager = Fragment.class
.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
FavGridAdapter
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class FavGridAdapter extends BaseAdapter {
private Context context;
int position1;
public FavGridAdapter(Context c,int position) {
context = c;
this.position1=position;
}
@Override
public int getCount() {
return GalImages.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(300, 300));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 0, 0, 0);
} else {
imageView = (ImageView) convertView;
}
FavList item = new FavList();
int i = item.getPosition();
Log.e("error", "getpos:"+i);
imageView.setImageResource(GalImages[i]);
return imageView;
}
private int[] GalImages = new int[] { R.drawable.gw12, R.drawable.gw1,
R.drawable.gw2, R.drawable.gw3, R.drawable.gw4, R.drawable.gw5,
R.drawable.gw6, R.drawable.gw7, R.drawable.gw8, R.drawable.gw9,
R.drawable.gw11, R.drawable.gw81 };
}
FavList
public class FavList {
int position;
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
public FavList() {
};
}
这是正确的方法吗?如果有其他技巧请告诉我。谢谢。