使用转到片段的 onclick 创建列表视图
Creating a listview with onclick which goes to a fragment
您好,我想创建一个列表视图,当您单击它时,它将转到一个片段。我尝试创建一个扩展到 BaseAdapter 的 CustomAdapter,但我遇到了片段和 getSupportFragmentManager() 的问题,它说 "Cannot resolve method "getSupportFragmentManager()"
这是我的 CustomAdapter 代码
public class CustomAdapter extends BaseAdapter {
Activity activity;
String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(MainActivity mainActivity, int[] prgmImages) {
context=mainActivity;
imageId=prgmImages;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return imageId.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public class Holder
{
ImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.menu_list, null);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.img.setImageResource(imageId[position]);
rowView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Fragment frag = new Fragment();
if (position == 0) {
} else if (position == 1) {
frag = new ServicesFrag();
} else if (position == 2) {
} else if (position == 3) {
} else if (position == 4) {
}
if (frag != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, frag).addToBackStack(null).commit();
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked " + position, Toast.LENGTH_LONG).show();
}
});
return rowView;
}
}
感谢您的帮助。
试试这个,
ListView listView = (ListView)findViewById(R.id.listView);
CustomAdapter adapter = new CustomAdapter(activity, array);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
Fragment frag = new Fragment();
if (position == 0) {
} else if (position == 1) {
frag = new ServicesFrag();
} else if (position == 2) {
} else if (position == 3) {
} else if (position == 4) {
}
if (frag != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, frag).addToBackStack(null).commit();
}
});
您好,我想创建一个列表视图,当您单击它时,它将转到一个片段。我尝试创建一个扩展到 BaseAdapter 的 CustomAdapter,但我遇到了片段和 getSupportFragmentManager() 的问题,它说 "Cannot resolve method "getSupportFragmentManager()"
这是我的 CustomAdapter 代码
public class CustomAdapter extends BaseAdapter {
Activity activity;
String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(MainActivity mainActivity, int[] prgmImages) {
context=mainActivity;
imageId=prgmImages;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return imageId.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public class Holder
{
ImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.menu_list, null);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.img.setImageResource(imageId[position]);
rowView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Fragment frag = new Fragment();
if (position == 0) {
} else if (position == 1) {
frag = new ServicesFrag();
} else if (position == 2) {
} else if (position == 3) {
} else if (position == 4) {
}
if (frag != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, frag).addToBackStack(null).commit();
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked " + position, Toast.LENGTH_LONG).show();
}
});
return rowView;
}
}
感谢您的帮助。
试试这个,
ListView listView = (ListView)findViewById(R.id.listView);
CustomAdapter adapter = new CustomAdapter(activity, array);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
Fragment frag = new Fragment();
if (position == 0) {
} else if (position == 1) {
frag = new ServicesFrag();
} else if (position == 2) {
} else if (position == 3) {
} else if (position == 4) {
}
if (frag != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, frag).addToBackStack(null).commit();
}
});