如何在RecyclerView Fragment中实现onActivityResult

时间:2016-06-09 10:11:08

标签: android-studio onclick material-design onactivityresult

我正在尝试在Tab中添加一个按钮,该按钮是上传图像活动的一部分,当点击活动中显示的2个标签之一内的按钮时(Tab 1 pics,T​​ab 2 Vid),用户可以选择来自设备的图像。

我的问题是实现onClick Listener和onActivityResult,它在onClick之后放置时没有使用,加上@Override被加下划线为错误。

我想我没有以正确的方式实现onClick,并且不确定如何以正确的方式激活onActivityResult,我将不胜感激任何指导;

MainActivty;

   public class upload extends AppCompatActivity {

public String UserID;
private static final int REQUEST_SIGNUP = 0;

private DrawerLayout mDrawerLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.upload);
    Firebase.setAndroidContext(this);

    // Adding Toolbar to Main screen
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // Setting ViewPager for each Tabs
    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);
    // Set Tabs inside Toolbar
    TabLayout tabs = (TabLayout) findViewById(R.id.tabs);
    tabs.setupWithViewPager(viewPager);
 }
  private void setupViewPager(ViewPager viewPager) {
    Adapter adapter = new Adapter(getSupportFragmentManager());
    adapter.addFragment(new UploadPictures(), "Pictures");
    adapter.addFragment(new UploadVideos(), "Videos");
    viewPager.setAdapter(adapter);
}

static class Adapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public Adapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

这是片段代码;

   public class UploadPictures extends android.support.v4.app.Fragment {
   @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    final RecyclerView recyclerView = (RecyclerView) inflater.inflate(
            R.layout.recycler_view, container, false);
   ContentAdapter adapter = new ContentAdapter(recyclerView.getContext());
    recyclerView.setAdapter(adapter);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    return recyclerView;

}

 public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public ImageView picture;
    public EditText tagEditText;
    public Button tagCurLoc, choosePic, uploadContent;


    public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
        super(inflater.inflate(R.layout.pictue_upload, parent, false));

        picture = (ImageView) itemView.findViewById(R.id.imgToUpload);
        tagEditText = (EditText) itemView.findViewById(R.id.tagEditText);
        tagCurLoc = (Button) itemView.findViewById(R.id.tagCurLoc);
        choosePic = (Button) itemView.findViewById(R.id.choosePic);
        choosePic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);

            }

            protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                if (data == null) {
                    onActivityResult(requestCode, resultCode, data);
                    Toast.makeText(getActivity().getApplicationContext(), "Canceled ", Toast.LENGTH_SHORT).show();


                } else {
                    if (requestCode == RESULT_LOAD_IMAGE) {
                            Uri selectedImgPath = data.getData();
                            picture.setImageURI(selectedImgPath);
                            RealFilePath = Uri.parse(String.valueOf(selectedImgPath));
                            Toast.makeText(getActivity().getApplicationContext(), " " + RealFilePath, Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

        uploadContent = (Button) itemView.findViewById(R.id.uploadContent);
    }
}



/**
 * Adapter to display recycler view.
 */
public class ContentAdapter extends RecyclerView.Adapter<ViewHolder> {
    // Set numbers of List in RecyclerView.

    private Context mContext;

    public ContentAdapter(Context context) {

        this.mContext = context;
    }



    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {

    }

    @Override
    public int getItemCount() {
        return LENGTH;
    }

}

不确定按钮的onClick侦听器是否应该在Adapter或View holder中实现,并且从那里可以使用onActivityResult的问题?

1 个答案:

答案 0 :(得分:0)

Weel,经过一些阅读和少量尝试后解决了。

onClick侦听器应该在ViewHolder类中实现,然后是

 @Override
  onActivityResult.

在onClickListener onActivityResult中调用;

 startActivityForResult(intent, RESULT_LOAD_IMAGE);

并在onActivityResult内;

 onActivityResult(requestCode, resultCode, data);

考虑所有这些都发生在扩展片段的活动中。