Android:onClick无法在viewPager中使用textView

时间:2016-03-09 18:03:11

标签: android android-fragments android-viewpager

如何在viewPage的页面中设置textView onClickListener?   我有一些包含许多textViews的页面。我希望在用户点击每个textView时共享和复制每个textView,但onClick不起作用... 这是我的PagerAdapter代码:

public class MyPagerAdapter extends PagerAdapter{

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return 11;
}


@Override
public Object instantiateItem(View collection, int position) {
    // TODO Auto-generated method stub

    LayoutInflater inflater = (LayoutInflater) collection.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    int resId=0;
    switch(position){
    case 0:resId=R.layout.one;break;
    case 1:resId=R.layout.two;break;
    case 2:resId=R.layout.three;break;
    case 3:resId=R.layout.four;break;
    case 4:resId=R.layout.five;break;
    case 5:resId=R.layout.six;break;
    case 6:resId=R.layout.seven;break;
    case 7:resId=R.layout.eight;break;
    case 8:resId=R.layout.nine;break;
    case 9:resId=R.layout.ten;break;
    case 10:resId=R.layout.eleven;break;
    }
    View view = inflater.inflate(resId, null);

    ((ViewPager) collection).addView(view, 0);

    return view;    
    }


@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
    // TODO Auto-generated method stub
    ((ViewPager) arg0).removeView((View) arg2);
    }


@Override
public Parcelable saveState() {
    // TODO Auto-generated method stub
    return null;
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
    // TODO Auto-generated method stub
     return arg0 == ((View) arg1);  }

}

和ViewPagerActivity:

@SuppressLint("NewApi") public class Main extends FragmentActivity implements OnClickListener{


ClipboardManager myClipboard;
   private ClipData myClip;

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);

        myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);


        Bundle getData=getIntent().getExtras();
        int position=getData.getInt("key");

        MyPagerAdapter adapter = new MyPagerAdapter();
        ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
        myPager.setAdapter(adapter);
        myPager.setCurrentItem(position)


 }

片段(这里我想将onClick设置为textView但不起作用):

public class One extends Fragment implements OnClickListener{
TextView tv;
Main main;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.one, container, false);
    tv=(TextView) rootView.findViewById(R.id.pageOnefirstDua);
    String text=tv.getText().toString();
    tv.setOnClickListener(this);
    return rootView;
}


@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId()){
    case R.id.pageOnefirstDua:

        Toast.makeText(getActivity(),"Clicked: ",Toast.LENGTH_SHORT).show();
        break;
    }
}

}

1 个答案:

答案 0 :(得分:0)

片段oncreateView返回View not ViewGroup, 请使用以下代码进行更新并检查,它应该可以正常工作。

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.one, container, false);
    tv=(TextView) rootView.findViewById(R.id.pageOnefirstDua);
    String text=tv.getText().toString();
    tv.setOnClickListener(this);
    return rootView;
}