Android按钮onclickListener不处理具有viewPager的片段

时间:2016-08-02 05:09:16

标签: android android-viewpager

我有一个带有ViewPager的简单片段,可以通过滑动来显示文本。

我正试图在下面显示的这个布局中获得按钮的OnClick事件,但我没有看到任何发生的事情,我是新手,不知道我是否遗漏了任何内容。

这是fragment_proposal.xml

<android.support.v4.view.ViewPager
    android:id="@+id/proposalViewPager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_below="@id/proposalHeader">                                        
</android.support.v4.view.ViewPager>

以下是此ViewPager的资源布局(proposal_swipe.xml),它将显示文本。

<Button
        android:id="@+id/proposalNotesBtn"
        android:layout_width="130dp"
        android:layout_height="50dp"
        android:layout_below="@id/notesText"
        android:text="Save Notes" />

<TextView
        android:id="@+id/enteredNotes"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_below="@id/proposalNotesBtn" />

这是我的片段类,我尝试捕获onclickevent

public class ProposalFragment extends Fragment implements View.OnClickListener {

ArrayList<ProposalDetailModel> listProposalDetail;
ViewPager viewPager;
ProposalSwipeAdapter proposalSwipeAdapter;
TextView tv, proposalHeader;
TextView sectionHeader;
Button proposalNotesBtn;
EditText notesText;
ProgressDialog progressDialog;

public ProposalFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View proposalDetailView = inflater.inflate(R.layout.fragment_proposal, container, false);
    proposalHeader = (TextView) proposalDetailView.findViewById(R.id.proposalHeader);
    proposalHeader.setTextColor(Color.WHITE);

    Bundle bundle = this.getArguments(); //getActivity().getIntent().getExtras();
    listProposalDetail = bundle.getParcelableArrayList("listDetails");

    View proposalView = inflater.inflate(R.layout.proposal_swipe, container, false);
    proposalNotesBtn = (Button) proposalView.findViewById(R.id.proposalNotesBtn);
    notesText = (EditText) proposalView.findViewById(R.id.notesText);

    proposalNotesBtn.setOnClickListener(new  View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.i("INSIDE CLICK", "INSIDE CLICK");
            new ProposalTask().execute();
        }
    });

    viewPager = (ViewPager) proposalDetailView.findViewById(R.id.proposalViewPager);
    viewPager.setAdapter(new ProposalSwipeAdapter(getContext(), listProposalDetail));
    return proposalDetailView;
}

我确信我错过了一些东西。我正在返回具有viewpager的布局视图。当我调试时,我看到proposalNotesBtn正在返回一个对象但是点击它,我看不到发生任何事情。请在这里提供帮助

这是我的ProposalSwipeAdapter类

public class ProposalSwipeAdapter extends PagerAdapter {

private ArrayList<String> imageResourcess = new ArrayList<>();
private Context context;
private LayoutInflater inflater;

public ProposalSwipeAdapter(Context context, ArrayList<ProposalDetailModel> list) {
    this.context = context;
    //this.imageResourcess = list;
    for (ProposalDetailModel objectProposal : list
            ) {

        this.imageResourcess.add("\t\t\t\t\t\t\t\t\t\t                       PRODUCT/SERVICE \n\n\n\nSERVICE/PRODUCT OFFERED:" + objectProposal.getIntroWhoAreWe() +
                "\n\nTYPE OF CUSTOMERS:" + objectProposal.getAddres() +
                "\n\nWHY WILL THEY BUY IT:" +
                "\n\nPRODUCT PRICE:" +
                "\n\nCOMPETITORS:" +
                "\n\nSERVICE DELIVERY:");
    }

}

@Override
public int getItemPosition(Object object) {
    int index = imageResourcess.indexOf(object);
    if (index == -1)
        return POSITION_NONE;
    else
        return index;
}

@Override
public int getCount() {
    return imageResourcess.size();
    //return imageResources.length;
}

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

@Override
public Object instantiateItem(ViewGroup container, int position) {
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemView = inflater.inflate(R.layout.proposal_swipe, null);
    //ImageView imageView = (ImageView) itemView.findViewById(R.id.proposalImage);
    //imageView.setImageResource(imageResources[position]);
    TextView tv = (TextView) itemView.findViewById(R.id.proposalText);
    tv.setText(imageResourcess.get(position).toString());
    //tv.setText(imageResources[position]);
    container.addView(itemView);
    Log.i("PROPOSAL VIEW-->", itemView.toString());
    return itemView;
}

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

4 个答案:

答案 0 :(得分:0)

我看到你做错了是你正在膨胀两个布局.projectDetailView和proposalView你只返回一个视图proposalDetailView到片段的布局。并且您的按钮位于proposalView中...您没有将此视图添加到片段中...还是将proposalView视为proposalDetailView的子项或您的目的......

答案 1 :(得分:0)

也许是因为按钮在另一个布局下?

尝试删除此行: 机器人:layout_below = “@ ID / notesText”

答案 2 :(得分:0)

您点击监听器的代码应该在PagerAdapter instantiateItem()

@Override
public Object instantiateItem(ViewGroup container, int position) {
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemView = inflater.inflate(R.layout.proposal_swipe, null);
    //ImageView imageView = (ImageView) itemView.findViewById(R.id.proposalImage);
    //imageView.setImageResource(imageResources[position]);
    TextView tv = (TextView) itemView.findViewById(R.id.proposalText);
    tv.setText(imageResourcess.get(position).toString());
    //tv.setText(imageResources[position]);

    Log.i("PROPOSAL VIEW-->", itemView.toString());
    proposalNotesBtn = (Button) itemView.findViewById(R.id.proposalNotesBtn);

    proposalNotesBtn.setOnClickListener(new  View.OnClickListener() {
       @Override
       public void onClick(View view) {
          Log.i("INSIDE CLICK", "INSIDE CLICK");
       }
    });
    container.addView(itemView);
    return itemView;
}

答案 3 :(得分:0)

移动此行viewPager.setAdapter(new ProposalSwipeAdapter(getContext(), listProposalDetail));

进入ProposalTask()的onPostExecute。