我遇到了listview与内部片段一起使用的问题,这些内部片段位于This git hub example中给出的导航抽屉页面滑动标签条视图标签中。
我正在使用相同的示例,并且所有4个选项卡都有一个列表视图,其中不同的arraylist设置为适配器。
我只使用一个片段和标签位置的基础我正在将不同的列表数据加载到内部片段arrayAdapter。我还有两个按钮,一个是删除,另一个是添加。
我想要什么:如果我按下添加按钮,那么它应该将新数据添加到数组列表中(基于选项卡位置将新添加添加到相应的arrayList)并刷新listview数据。
在我的代码中刷新当前查看选项卡,但在下一个选项卡列表视图中还刷新并获取上一个选项卡的数据。请帮助我,任何帮助将不胜感激。
我有单一类文件。如果我的方法有误,请告诉我正确的方法。
public class PageSlidingTabStripFragment扩展了Fragment {
public static final String TAG = PageSlidingTabStripFragment.class
.getSimpleName();
private MyListAdapter myAdapter;
private boolean isDeleteBtnClicked = false;
private int tabType = 0;
private ListView myListView;
public static PageSlidingTabStripFragment newInstance() {
return new PageSlidingTabStripFragment();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.pager, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) view
.findViewById(R.id.tabs);
ViewPager pager = (ViewPager) view.findViewById(R.id.pager);
MyPagerAdapter adapter = new MyPagerAdapter(getChildFragmentManager());
pager.setAdapter(adapter);
tabs.setViewPager(pager);
}
public class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
private final String[] TITLES = { "Categories", "Home", "Top Paid",
"Top Free" };
@Override
public CharSequence getPageTitle(int position) {
return TITLES[position];
}
@Override
public int getCount() {
return TITLES.length;
}
@Override
public SherlockFragment getItem(int position) {
return new SuperAwesomeCardFragment().newInstance(position); // here I am calling the fragment by sending the position
}
}
private ArrayList<String> categoriesList = new ArrayList<String>();
private ArrayList<String> homeList = new ArrayList<String>();
private ArrayList<String> topPaidList = new ArrayList<String>();
private ArrayList<String> topFreeList = new ArrayList<String>();
@SuppressLint("ValidFragment")
public class SuperAwesomeCardFragment extends SherlockFragment{
private final String ARG_POSITION = "position";
private int position;
private Button deleteBtn;
private Button addBtn;
public SuperAwesomeCardFragment newInstance(int position) {
SuperAwesomeCardFragment f = new SuperAwesomeCardFragment();
Bundle b = new Bundle();
b.putInt(ARG_POSITION, position);
f.setArguments(b);
return f;
}
public SuperAwesomeCardFragment(){
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
categoriesList.clear();
// adding element to Categories arraylist
categoriesList.add("categories -1");
categoriesList.add("categories -2");
categoriesList.add("categories -3");
categoriesList.add("categories -4");
homeList.clear();
// adding element to Home arraylist
homeList.add("home -1");
homeList.add("home -2");
homeList.add("home -3");
homeList.add("home -4");
topPaidList.clear();
// adding element to TopPaid arraylist
topPaidList.add("topPaid -1");
topPaidList.add("topPaid -2");
topPaidList.add("topPaid -3");
topPaidList.add("topPaid -4");
topFreeList.clear();
// adding element to TopFree arraylist
topFreeList.add("topFree -1");
topFreeList.add("topFree -2");
topFreeList.add("topFree -3");
topFreeList.add("topFree -4");
position = getArguments().getInt(ARG_POSITION); // here I get the position of tab/view pager
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.main_pager_body, container, false);
myListView = (ListView) root.findViewById(R.id.my_list_view);
deleteBtn = (Button) root.findViewById(R.id.delete_btn);
addBtn = (Button) root.findViewById(R.id.add_btn);
switch (position) {
// Categories position
case 0:
setListView(categoriesList);
break;
// Home position
case 1:
setListView(homeList);
break;
// TopPaid position
case 2:
setListView(topPaidList);
break;
// TopFree position
case 3:
setListView(topFreeList);
break;
} // here I am setting the list view based on the tab/view pager position.
deleteBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "deleteBtn Button clicked", Toast.LENGTH_SHORT).show();
}
});
addBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), " addBtn Button clicked", Toast.LENGTH_SHORT).show();
switch (position) {
// Categories position
case 0:
categoriesList.add("categories -5");
setListView(categoriesList);
break;
// Home position
case 1:
homeList.add("home -5");
setListView(homeList);
break;
// TopPaid position
case 2:
topPaidList.add("topPaid -5");
setListView(topPaidList);
break;
// TopFree position
case 3:
topFreeList.add("topFree -5");
setListView(topFreeList);
break;
}
}
});
return root;
}
public void setListView(ArrayList<String> myList){
myAdapter = new MyListAdapter(getActivity().getApplicationContext(), R.layout.list_row, myList);
myListView.setAdapter(myAdapter);
myListView.setItemsCanFocus(true);
myAdapter.notifyDataSetChanged();
}
}
public class MyListAdapter extends ArrayAdapter<String>
{
ArrayList<String> myList;
public MyListAdapter(Context context, int textViewResourceId,
ArrayList<String> myList) {
super(context, textViewResourceId, myList);
this.myList = myList;
}
public class ViewHolder {
private TextView listElementTV;
private RelativeLayout buttonContains;
private Button deleteItemBtn;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder = null;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// get the view for assign data to list view
v = vi.inflate(R.layout.list_row, null);
TextView listElementTV = (TextView) v.findViewById(R.id.list_element);
RelativeLayout buttonContains = (RelativeLayout) v.findViewById(R.id.button_contains);
Button deleteItemBtn = (Button) v.findViewById(R.id.delete_item);
holder = new ViewHolder();
holder.listElementTV = listElementTV;
holder.buttonContains = buttonContains;
holder.deleteItemBtn = deleteItemBtn;
v.setTag(holder);
} else
holder = (ViewHolder) v.getTag();
// get list of hospitalityInfo using position
String listElement = this.myList.get(position);
if(listElement != null){
holder.listElementTV.setText(listElement);
}
return v;
}
}
}
答案 0 :(得分:4)
Android学习者
回答太迟了,但可能有助于其他人
用于解决此类实施的链接下方的用户。