【问题标题】:Can't use onClickImageListener on a Fragment swipetabs不能在 Fragment swipetabs 上使用 onClickImageListener
【发布时间】:2016-07-19 11:21:47
【问题描述】:

我一直在尝试将图像添加到滑动标签的片段中,该片段将被单击以打开另一个活动,但它还没有工作!

我有一个滑动选项卡活动,它有三个片段,首先是介绍,其次是要制作菜单,第三是信息,但我无法制作该菜单,我希望手动制作该菜单的图像,然后单击里面的下一个相关活动。帮帮我

片段代码:-

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;


public class TwoFragment extends Fragment {
ImageView img;
public TwoFragment() {
    // Required empty public constructor
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_two, container, false);
    img = (ImageView)getView().findViewById(R.id.imageView);
    img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(TwoFragment.this, Programming.class);
            startActivity(intent);
     }
  });


  }
}

添加图片:Image of fragment in which the image will be clicked

【问题讨论】:

    标签: android android-layout android-studio android-fragments android-intent


    【解决方案1】:

    不是TwoFragment.this作为Intent的第一个参数,而是像这样传递getActivity()

    @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), Programming.class);
            startActivity(intent);
     }
    

    说明:第一个参数的类型应该是Context,这就是传递活动有效的原因,原因是活动扩展了上下文(间接)。片段没有,因此在那里传递片段将不起作用。

    【讨论】:

      【解决方案2】:
        public void onClick(View v) {
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
                  transaction.replace(R.id.layoutid, Programming.class);
                  transaction.commit();
      

      }

          Here 'R.id.layoutid' is your main layout id of R.layout.fragment_two layout file.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-04-26
        • 1970-01-01
        • 1970-01-01
        • 2017-06-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多