当前位置: 代码迷 >> Android >> Android 在一个Activity里面如何让两个gallery联动,就是同时移动
  详细解决方案

Android 在一个Activity里面如何让两个gallery联动,就是同时移动

热度:63   发布时间:2016-05-01 21:15:02.0
Android 在一个Activity里面怎么让两个gallery联动,就是同时移动
这是MainActivity

public class MainActivity extends Activity implements
AdapterView.OnItemSelectedListener {
private Gallery photo;
private Gallery details;
private PhotoAdapter photoAdapter;
private DetailsAdapter detailsAdapter;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.audio_player);
setupViews();
}

private void setupViews() {
PersonService ps = new PersonService(this);
List<Person> list = ps.getData();

photo = (Gallery) findViewById(R.id.gallery);
photoAdapter = new PhotoAdapter(this,list);
photo.setAdapter(photoAdapter);
photo.setOnItemSelectedListener(this);

details = (Gallery) findViewById(R.id.gallery2);
detailsAdapter = new DetailsAdapter(this,list);
details.setAdapter(detailsAdapter);
details.setOnItemSelectedListener(this);

}

public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {

if (parent == details) {
// 更新第一个Gallery
detailsAdapter.notifyDataSetChanged(position);
// photo.setSelection(position);
Log.i("MainActivity", view.getId()+"");
} else if (parent == photo) {
// 更新第二个Gallery
photoAdapter.notifyDataSetChanged(position);
// details.setSelection(position);
Log.i("MainActivity", view.getId()+"");
}
}

public void onNothingSelected(AdapterView<?> parent) {

}
}

这是第一个gallery的adapter


public class PhotoAdapter extends BaseAdapter {

private Context context;
private List<Person> list;
private Person person;

public PhotoAdapter(Context context,List<Person> list) {
this.context = context;
this.list = list;
}

public int getCount() {
if (list != null) {
return list.size();
}
return 0;
}

public Object getItem(int position) {
if (list != null) {
return list.get(position);
}
return null;
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
person = list.get(position);
ImageView iv = new ImageView(context);
iv.setLayoutParams(new Gallery.LayoutParams(100, 100));
iv.setAdjustViewBounds(true);
iv.setImageBitmap(person.photo);
return iv;
}

public void notifyDataSetChanged(int position) {

Log.i("PhotoAdapter","notifyDataSetChanged: "+position);
super.notifyDataSetChanged();
}

}

这是第二个gallery的adapter
public class DetailsAdapter extends BaseAdapter {
private Context mContext;
private List<Person> list;
private LayoutInflater inflater ;
private Person person = null;

public DetailsAdapter(Context context,List<Person> list) {
this.inflater = LayoutInflater.from(context);
this.mContext = context;
this.list = list;
}

public int getCount() {
if (list != null) {
return list.size();
}
return 0;
}

public Object getItem(int position) {
if (list != null) {
return list.get(position);
}
return null;
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
person = list.get(position); // 
ViewHolder mView = null;
if(convertView==null){
convertView = inflater.inflate(R.layout.list_item, null);
mView = new ViewHolder();
mView.listLorder = (ImageView)convertView.findViewById(R.id.list_order);
  相关解决方案