Android 如何获取选项卡名称

Android 如何获取选项卡名称,android,listview,Android,Listview,我正在使用SlidingTableOut。共有5个选项卡,我使用来自不同json请求的listview填充每个选项卡。单击列表视图中的一项,我需要获取选项卡的名称或id。您需要向我们提供您当前的代码或实现,这将为我们提供一些如何帮助您的指导 但我对您的实现的第一个猜测是:我假设当您说SlidingTabLayout时,您指的是android开发人员概述的代码: 如果是这种情况,则在单击选项卡并在单击侦听器中注册选项卡时,可以从查看页面获取选项卡名称: private class TabClick

我正在使用SlidingTableOut。共有5个选项卡,我使用来自不同json请求的listview填充每个选项卡。单击列表视图中的一项,我需要获取选项卡的名称或id。您需要向我们提供您当前的代码或实现,这将为我们提供一些如何帮助您的指导

但我对您的实现的第一个猜测是:我假设当您说SlidingTabLayout时,您指的是android开发人员概述的代码:

如果是这种情况,则在单击选项卡并在单击侦听器中注册选项卡时,可以从查看页面获取选项卡名称:

private class TabClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            for (int i = 0; i < mTabStrip.getChildCount(); i++) {
                if (v == mTabStrip.getChildAt(i)) {
                    mViewPager.setCurrentItem(i);
                    //Get tab name from mViewPager - differs based on your implementation
                    return;
                }
            }
        }
    }
私有类选项卡ClickListener实现View.OnClickListener{
@凌驾
公共void onClick(视图v){
对于(int i=0;i
您需要向我们提供您当前的代码或实现-这将为我们提供一些如何帮助您的方向

但我对您的实现的第一个猜测是:我假设当您说SlidingTabLayout时,您指的是android开发人员概述的代码:

如果是这种情况,则在单击选项卡并在单击侦听器中注册选项卡时,可以从查看页面获取选项卡名称:

private class TabClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            for (int i = 0; i < mTabStrip.getChildCount(); i++) {
                if (v == mTabStrip.getChildAt(i)) {
                    mViewPager.setCurrentItem(i);
                    //Get tab name from mViewPager - differs based on your implementation
                    return;
                }
            }
        }
    }
私有类选项卡ClickListener实现View.OnClickListener{
@凌驾
公共void onClick(视图v){
对于(int i=0;i
是的,这是android开发者手册中列出的吗 好的,情况是这样的

     @Override
            public Object instantiateItem(ViewGroup container, int position) {

                View view=null;
                if(position == 0){
                   view = getActivity().getLayoutInflater().inflate(R.layout.pager_item,container, false);
                    homeList = (ListView) view.findViewById(R.id.home_list);
                    queryImp(view, "");
                   homeJSONAdapter = new JSONAdapter(getActivity(), getActivity().getLayoutInflater());

                    homeList.setAdapter(homeJSONAdapter);
                    homeList.setOnItemClickListener(this);


                }else{
                    view = getActivity().getLayoutInflater().inflate(R.layout.other_pager_item,container, false);
                    otherList = (ListView) view.findViewById(R.id.other_list);
                    queryImp(view, "other");
                    otherJSONAdapter = new JSONAdapterOther(getActivity(), getActivity().getLayoutInflater());

                    otherList.setAdapter(ekonomiaJSONAdapter);
                    otherList.setOnItemClickListener(this);

                }

                container.addView(view);

                view.findViewById(R.id.item_title);

                return view;
            }

this is the code that populates the tabs on SlidingTabsBasicFragment and the following is the onclick for the items of the lists

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


            if(position == 0)
            {

                JSONObject jsonObject = (JSONObject) homeJSONAdapter.getItem(position);
                String imageURL = jsonObject.optString("img_url");
                String artText = jsonObject.optJSONObject("content").optString("rendered");
                String artTitle = jsonObject.optJSONObject("title").optString("rendered");


                Intent detailIntent = new Intent(getActivity(), SingleArticle.class);


                detailIntent.putExtra("imgURL", imageURL);
                detailIntent.putExtra("articleText", artText);
                detailIntent.putExtra("articleTitle", artTitle);

                startActivity(detailIntent);

            }else{


                JSONObject jsonObject1 = (JSONObject) otherJSONAdapter.getItem(position);
                String imageURL1 = jsonObject1.optString("img_url");
                String artText1 = jsonObject1.optJSONObject("content").optString("rendered");
                String artTitle1 = jsonObject1.optJSONObject("title").optString("rendered");

               y
                Intent detailIntent1 = new Intent(getActivity(), SingleArticleOther.class);


                detailIntent1.putExtra("imgURL1", imageURL1);
                detailIntent1.putExtra("articleText1", artText1);
                detailIntent1.putExtra("articleTitle1", artTitle1);


                startActivity(detailIntent1);

            }
@覆盖
公共对象实例化项(视图组容器,int位置){
视图=空;
如果(位置==0){
视图=getActivity().GetLayoutFlater().充气(R.layout.pager_项,容器,false);
homeList=(ListView)view.findViewById(R.id.home\u list);
queryImp(视图“”);
homeJSONAdapter=newJSONAdapter(getActivity(),getActivity().getLayoutFlater());
setAdapter(homeJSONAdapter);
homeList.setOnItemClickListener(此);
}否则{
视图=getActivity().GetLayoutFlater().充气(R.layout.other_pager_item,container,false);
otherList=(ListView)view.findViewById(R.id.other_list);
queryImp(视图,“其他”);
otherJSONAdapter=newJSONAdapterOther(getActivity(),getActivity().getLayoutFlater());
setAdapter(ekonomiaJSONAdapter);
otherList.setOnItemClickListener(this);
}
container.addView(视图);
视图。findViewById(R.id.项目名称);
返回视图;
}
这是填充SlidingAbbasicFragment选项卡的代码,下面是列表项的onclick
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
如果(位置==0)
{
JSONObject JSONObject=(JSONObject)homeJSONAdapter.getItem(位置);
字符串imageURL=jsonObject.optString(“img_url”);
字符串artText=jsonObject.optJSONObject(“内容”).optString(“呈现”);
字符串artTitle=jsonObject.optJSONObject(“title”).optString(“呈现”);
Intent detailIntent=新的意图(getActivity(),SingleArticle.class);
detailIntent.putExtra(“imgURL”,imageURL);
detailIntent.putExtra(“articleText”,artText);
detailIntent.putExtra(“articleTitle”,artTitle);
星触觉(意向);
}否则{
JSONObject JSONObject 1=(JSONObject)otherJSONAdapter.getItem(位置);
字符串imageURL1=jsonObject1.optString(“img_url”);
字符串artText1=jsonObject1.optJSONObject(“内容”).optString(“呈现”);
字符串artTitle1=JSONObject 1.optJSONObject(“标题”).optString(“呈现”);
Y
Intent detailIntent1=新的意图(getActivity(),SingleArticleOther.class);
detailIntent1.putExtra(“imgURL1”,imageURL1);
detailIntent1.putExtra(“articleText1”,artText1);
详细意图1.putExtra(“条款标题1”,第1条);
起始触觉(1);
}
基本上,它所做的只是点击列表中的项目,然后进入全文阅读。由于某种原因,它变得一团糟。(当我记录位置时,它得到的是列表中项目的位置,而不是选项卡的位置)当我试图点击homeJsonAdapter的一个项目时,它会转到otherJsonAdapter的文章,我不知道为什么。
因此,我想做的是获取listview所在选项卡的名称,并基于该名称创建JsonAdapter,然后获取文章

是的,这是android开发者手册中列出的吗 好的,情况是这样的

     @Override
            public Object instantiateItem(ViewGroup container, int position) {

                View view=null;
                if(position == 0){
                   view = getActivity().getLayoutInflater().inflate(R.layout.pager_item,container, false);
                    homeList = (ListView) view.findViewById(R.id.home_list);
                    queryImp(view, "");
                   homeJSONAdapter = new JSONAdapter(getActivity(), getActivity().getLayoutInflater());

                    homeList.setAdapter(homeJSONAdapter);
                    homeList.setOnItemClickListener(this);


                }else{
                    view = getActivity().getLayoutInflater().inflate(R.layout.other_pager_item,container, false);
                    otherList = (ListView) view.findViewById(R.id.other_list);
                    queryImp(view, "other");
                    otherJSONAdapter = new JSONAdapterOther(getActivity(), getActivity().getLayoutInflater());

                    otherList.setAdapter(ekonomiaJSONAdapter);
                    otherList.setOnItemClickListener(this);

                }

                container.addView(view);

                view.findViewById(R.id.item_title);

                return view;
            }

this is the code that populates the tabs on SlidingTabsBasicFragment and the following is the onclick for the items of the lists

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


            if(position == 0)
            {

                JSONObject jsonObject = (JSONObject) homeJSONAdapter.getItem(position);
                String imageURL = jsonObject.optString("img_url");
                String artText = jsonObject.optJSONObject("content").optString("rendered");
                String artTitle = jsonObject.optJSONObject("title").optString("rendered");


                Intent detailIntent = new Intent(getActivity(), SingleArticle.class);


                detailIntent.putExtra("imgURL", imageURL);
                detailIntent.putExtra("articleText", artText);
                detailIntent.putExtra("articleTitle", artTitle);

                startActivity(detailIntent);

            }else{


                JSONObject jsonObject1 = (JSONObject) otherJSONAdapter.getItem(position);
                String imageURL1 = jsonObject1.optString("img_url");
                String artText1 = jsonObject1.optJSONObject("content").optString("rendered");
                String artTitle1 = jsonObject1.optJSONObject("title").optString("rendered");

               y
                Intent detailIntent1 = new Intent(getActivity(), SingleArticleOther.class);


                detailIntent1.putExtra("imgURL1", imageURL1);
                detailIntent1.putExtra("articleText1", artText1);
                detailIntent1.putExtra("articleTitle1", artTitle1);


                startActivity(detailIntent1);

            }
@覆盖
公共对象实例化项(视图组容器,int位置){
视图=空;
如果(位置==0){
视图=getActivity().GetLayoutFlater().充气(R.layout.pager_项,容器,false);
homeList=(ListView)view.findViewById(R.id.home\u list);