为什么ViewPager在android中出错?

时间:2016-01-18 09:45:02

标签: android android-viewpager

我试图在任何地方找到答案,但我没有找到答案。

说明:我的片段中有一个Viewpager。我将一个适配器调用到我的片段中并将其设置为ViewPager。根据JSONArray数据,我的ViewPager是动态的。我有一个jsonArray,它有五个对象。我将每个对象添加到List中并将其传递给适配器。所以,我的viewpager是根据list.size()创建的,但问题是我的HomeFragment.java已经加载了。适配器被调用两次(不确定)但日志显示它被调用两次。

这是我的Homefragment.java

private class TabJson extends AsyncTask<String,String,String> {

    String jsonStr="";

    private static final String url="http://10.0.2.2/JSON/recentmatch.php";
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {

        ServiceHandler sh=new ServiceHandler();
        jsonStr=sh.makeServiceCall(url,ServiceHandler.POST);
        try{
            JSONObject obj=new JSONObject(jsonStr);
            JSONObject data=obj.getJSONObject("data");
            JSONArray card=data.getJSONArray("cards");

            PagerLength=0;
            lst_obj=new ArrayList<>();
            for(int i=0;i<card.length();i++){
                JSONObject card_obj=card.getJSONObject(i);

                if(card_obj.getString("status").equals("started") || card_obj.getString("status").equals("notstarted")){
                    PagerLength++;
                    lst_obj.add(card_obj);
                }
            }
        }
        catch (JSONException e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        LiveAdapter adapterTabRecent=new LiveAdapter(getContext(),lst_obj);
        pagerRecentMatches.setAdapter(adapterTabRecent);
        pagerRecentMatches.setCurrentItem(0);

        float scaledDensity = getResources().getDisplayMetrics().scaledDensity;
        LinearLayout.LayoutParams param1=new LinearLayout.LayoutParams(10*(int)scaledDensity,10*(int)scaledDensity);
//        for(int i=0;i<PagerLength-1;i++){
//            ImageView img=new ImageView(getContext());
//            TextView space=new TextView(getContext());
//            img.setId(R.id.selected_id);
//            img.setLayoutParams(param1);
//            img.setImageResource(R.drawable.unselect);
//            space.setText(" ");
//            selector_dynamic.addView(img);
//        }
    }
}

上面是我从JSON获取数据的代码,我将其设置为适配器。

这是我的LiveAdapter.java

public class LiveAdapter extends PagerAdapter{

    private Context context;
    List<JSONObject> lst;
    JSONObject mainObj=null;
    View itemView;
    private LayoutInflater inflater;
    String status="";
    public LiveAdapter(Context context,List<JSONObject> lst){
        this.context=context;
        this.lst=lst;
        inflater=(LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        return lst.size();
    }

    @Override
    public int getItemPosition(Object object) {
        int index=lst.indexOf((List)object);

        if(lst.contains((List)object)){
            return index;
        }
        else{
            return POSITION_NONE;
        }
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==((LinearLayout)object);
    }

    private class Holder{
        TextView txt_one_country_name;
        TextView txt_two_country_name;
        TextView txt_venue;
        TextView match;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        itemView=inflater.inflate(R.layout.tab_slider,container,false);
        Holder holder=new Holder();
        String[] parts;
        String[] state_parts;
        String[] match_parts;

        Log.e("POSITION",""+position);
        mainObj=this.lst.get(position);

        holder.txt_one_country_name=(TextView)itemView.findViewById(R.id.txt_one_country_name);
        holder.txt_two_country_name=(TextView)itemView.findViewById(R.id.txt_two_country_name);
        holder.txt_venue=(TextView)itemView.findViewById(R.id.venue);
        holder.match=(TextView)itemView.findViewById(R.id.match);

        try {

            status=mainObj.getString("status");
            Log.e("POSITION INSIDE TRY",""+position);
            Log.e("Status-->>>",""+status);
            String name = mainObj.getString("name");
            String state=mainObj.getString("venue");
            String title=mainObj.getString("title");

            parts=name.split("vs");
            match_parts=title.split("-");
            state_parts=state.split(",");

            holder.txt_one_country_name.setText(parts[0]);
            holder.txt_two_country_name.setText(parts[1]);
            holder.txt_venue.setText(state_parts[1]);
            holder.match.setText(match_parts[1]);
        }
        catch (JSONException e){
            e.printStackTrace();
        }
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(status.equals("notstarted")) {
                    Log.e("STATUS INSIDE ONCLICK",""+status);
                    Intent intent = new Intent(context, NotStartedMatchDetails.class);
                    context.startActivity(intent);
                }

            }
        });
        container.addView(itemView);
        return itemView;
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
//        super.setPrimaryItem(container, position, object);
        itemView=(View)object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((LinearLayout)object);
    }
}

我在instantiateItem()中设置了日志。 当我运行默认设置为0的应用程序位置时为0和1。

这是我的位置值

01-18 15:47:02.295 31593-31593/com.example.myworld.materialdesign E/POSITION: 0
01-18 15:47:02.295 31593-31593/com.example.myworld.materialdesign E/POSITION INSIDE TRY: 0
01-18 15:47:02.295 31593-31593/com.example.myworld.materialdesign E/Status-->>>: notstarted
01-18 15:47:02.306 31593-31593/com.example.myworld.materialdesign E/POSITION: 1
01-18 15:47:02.306 31593-31593/com.example.myworld.materialdesign E/POSITION INSIDE TRY: 1
01-18 15:47:02.306 31593-31593/com.example.myworld.materialdesign E/Status-->>>: started

问题是当我从右向左滑动时,下面提到的位置值: 我动态创建了五个标签,它的价值在于 2,3,4就是这样。哪里是0和1?

但是当我从左向右滑动时,下面提到的位置值: 2,1,0那就是3和4?

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

这样做 实例化对象时

@Override
public Object instantiateItem(ViewGroup container, int position) {
.....
itemView.setTag(position);
.....

}
//now on click of the item view you can get it back as follows
itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int position = (int) v.getTag();// v here is your itemView
}