我目前有一个带有ImageButton的片段,可以打开DialogFragment。此DialogFragment是一个使用适配器的自定义列表,其中每个条目都有一个ImageView和TextView。
是否可以将所选项目的ImageView的“src”返回到打开DialogFragment的Fragment?我需要将ImageButton的src更改为所选图像。
我的主要片段调用DialogFragment:
ImageButton starter = (ImageButton) myView.findViewById(R.id.imageStarterItem);
starter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//Here
ItemPhysStarterFragment dialog = ItemPhysStarterFragment.newInstance();
dialog.show(getFragmentManager(), "fragmentDialog");
}
});
我的DialogFragment类包含ListView:
public class ItemPhysStarterFragment extends DialogFragment {
Button btn;
ListView lv;
SearchView sv;
ArrayAdapter<String> adapter;
String[] names={"Bumba's Mask", "Rangda's Mask", "Bluestone Pendant", "Death's Toll",
"Mark of the Vanguard", "Swift Wing", "War Flag", "Watcher's Gift"};
int[] images={R.drawable.bumbas_mask, R.drawable.rangdas_mask, R.drawable.bluestone_pendant, R.drawable.deaths_toll,
R.drawable.mark_of_the_vanguard, R.drawable.swift_wing, R.drawable.war_flag, R.drawable.watchers_gift};
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myView=inflater.inflate(R.layout.item_list, null);
//SET TITLE DIALOG TITLE
getDialog().setTitle("Starter Items");
//BUTTON,LISTVIEW,SEARCHVIEW INITIALIZATIONS
lv=(ListView) myView.findViewById(R.id.listView1);
sv=(SearchView) myView.findViewById(R.id.searchView1);
btn=(Button) myView.findViewById(R.id.dismiss);
//CREATE AND SET ADAPTER TO LISTVIEW
final Adapter adapter = new Adapter(getActivity(), this.getItems());
lv.setAdapter(adapter);
//SEARCH
sv.setQueryHint("Search starter items..");
sv.setOnQueryTextListener(new OnQueryTextListener() {
public boolean onQueryTextSubmit(String txt) {
return false;
}
public boolean onQueryTextChange(String txt) {
adapter.getFilter().filter(txt);
return false;
}
});
//BUTTON
btn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
dismiss();
}
});
return myView;
}
public static ItemPhysStarterFragment newInstance(){
ItemPhysStarterFragment f = new ItemPhysStarterFragment();
return f;
}
private ArrayList<Item> getItems() {
ArrayList<Item> items = new ArrayList<Item>();
Item item;
for(int i = 0; i < names.length; i++)
{
item = new Item(names[i], images[i]);
items.add(item);
}
return items;
}
}
答案 0 :(得分:4)
首先创建一个界面
public interface OnListItemClickedListener {
void onListItemClick(int resourceId);
}
为interface
public class ItemPhysStarterFragment extends DialogFragment {
OnListItemClickedListener mListener;
public void setOnListItemSelectedListener(OnListItemClickedListener listener) {
this.mListener = listener;
}
}
现在将听众设置为fragment
ItemPhysStarterFragment dialog = ItemPhysStarterFragment.newInstance();
dialog.setOnListItemSelectedListener(new OnListItemClickedListener() {
@Override
public void onListItemClick(int resourceId) {
// User your resourceId
}
});
dialog.show(getFragmentManager(), "fragmentDialog");
最后一步,在ItemPhysStarterFragment
片段的ListItemClick()中触发侦听器 -
...public void onListItemClick(int resourceId) {
mListener.onListItemClick(pass your resource id);
}
答案 1 :(得分:1)
这个link对我有助于达到@Wizzard提出的相同结果。
它基本上提出了相同的解决方案,除了不需要创建自定义接口。
在fragment class
创建变量和setter方法以接受OnItemClickListener
private android.widget.AdapterView.OnItemClickListener onItemClickListener;
public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
在fragement class
中,确保listView
设置项目点击监听器:
list.setOnItemClickListener(onItemClickListener);
最后,您main class
可以致电listviewItemClickListener
:
DialogMyListFragment df = new DialogMyListFragment().getInstance();
df.setOnItemClickListener(listener);
答案 2 :(得分:0)
您可以声明一个界面
public interface OnCustomItemClickListener {
void onItemClicked(final String src);
}
并在创建对话框时实现该界面
(...),new OnCustomItemClickListener() {
@Override
public void onItemClicked() {
//do something
}
}, (...)
并在ItemPhysStarterFragment类
上正确处理它