公共类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;
}
}
}
问题内容: 我有以下代码: MainActivity.java TabsPagerAdapter.java 我正在制作一个图书馆应用程序,其中导航是通过选项卡进行的,问题是每次我从第三个选项卡转到第一个或第一个至第三个时,选项卡的内容都在刷新,我想防止刷新,任何请帮助 ? 问题答案: 默认情况下,在滑动页面时会重新创建片段。为防止这种情况,您可以尝试以下三种方法之一: 1. 在您的片段中,呼叫。
我在我的项目中使用并以片段形式显示数据。这是一个很棒的图书馆,工作很棒。每个片段都由一个学者的讲座组成,这些讲座是从web服务加载的。访问web服务的代码位于片段的方法中。目前,所有片段的数据在添加到时同时加载。我想知道是否只有当用户单击/滑动到特定的选项卡时才能加载片段的数据。不是一次什么都做。 当前的代码是相当标准的,我从提供的示例[android解决方案]中获得了帮助。1 为了确保Fragm
} 我使用android ViewPager,当我点击不同的标签时,使用不同的片段来加载。但问题是,当我单击选项卡时,片段没有加载。当活动加载时,所有的片段都被加载,那么片段就不会改变。我如何解决这个问题?我需要加载片断时,我选择选项卡在查看页。
我已经在我的片段中重写了以下方法:setUserVisibleHint 在fragment类中,变量设置为:private var isLoadOnce=false。 我有3个片段,问题是当我的活动popsup时,第一个片段是可见的,如果我单击最后一个选项卡,也就是第三个选项卡加载第三个片段,什么也不会发生,即web服务根本不会调用。
我试图在片段中创建自定义listView,我搜索了其他问题,但找不到任何东西。我的列表适配器是这样的 我的片段类是这样的: } 问题是这部分:CustomList adapter=新CustomList(tab1.this,string,imageId);构造函数(public CustomList(活动上下文,字符串[]web,整数[]imageId))只接受活动 我怎样才能解决这个问题?
在html中,当创建选项卡时,我可以将Paris选项卡设置为活动的(在html中用“style=”display:block;“硬编码)。因此,当页面加载时,活动选项卡(Paris)将显示。 问题 当我点击不同的标签(伦敦)并点击刷新,它再次显示巴黎。我如何才能点击刷新并显示当前活动的选项卡的信息?而不是将我带回定义的选项卡。也许javascript或jquery可以解决我的问题? 柱塞视图 示例