如何从OnClickListener内部更新ViewPager?

时间:2018-10-10 00:05:37

标签: android android-viewpager

我正在使用SwipeCards库,并且主要代码在阵列适配器内部。一个Activity(BaseCardsActivity)从Firebase感染一些数据,并将其传递给ArrayAdapter(SwingAdapterClass)。该适配器内部有一些图像,当用户单击其中的一个时,此ImageView消失,并且Interface(此处未显示)根据用户单击的img获取一些图像并将其全部加载到ViewPager。

问题是:我需要从附加到每个ImageView的OnClickListener方法内部加载Viewpager(原因太长,并且这个问题已经太长),因此仅当用户单击img时,我才可以请求Firebase。

但是,没有从OnClickListener内部加载ViewPager。从OnClickListener外部,它确实可以正常工作,如我在示例中所示。

我提供了一个更精简的逻辑示例,否则这篇文章太长了。请帮忙。抱歉,我在这里编辑代码太糟糕了。

谢谢。

BaseCardsActivity.java

//implement the onFlingListener
public class BaseCardsActivity extends AppCompatActivity{
    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_base_cards);
       //mAuth, DatabaseReference, etc.

        rowItem = new ArrayList<CardsModel>();
        mAdapterClass = new SwingAdapterClass(BaseCardsActivity.this, R.layout.cards,rowItem);
 final SwipeFlingAdapterView flingContainer = (SwipeFlingAdapterView) findViewById(R.id.frame);
  //  flingContainer.setAdapter(mAdapterClass);
    flingContainer.setAdapter(mViewpagerEvents);
        instantiateSwipeCards();

}

private void instantiateSwipeCards(){
   mDatabaseReference.addChildEventListener(new ChildEventListener() {
    //datasnapshot....
    // get all images I need
    // put images in List<String> imgList

    CardsModel cards = new CardsModel(imgList);
    rowItem.add(cardsModel);

    mAdapterClass.notifyDataSetChanged();

  }
}

activity_base_cards.xml:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <com.lorentzos.flingswipe.SwipeFlingAdapterView
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffeee9e2"
        app:rotation_degrees="15.5"
        tools:context=".BaseCardsActivity"/>

</RelativeLayout>

SwingAdapterClass.java

public class SwingAdapterClass extends ArrayAdapter<CardsModel>{

 public SwingAdapterClass(@NonNull Context context, int resource, List<CardsModel> item) {
        super(context, resource, item);
        mContext = context;    
  }

 @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        final CardsModel cardsModel = getItem(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.cards, parent, false);
        }


   mPager = convertView.findViewById(R.id.viewpager);
      mImageView = convertView.findViewById(R.Id.clickImg);
      mImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {                    
mCircleImageView.setVisibility(View.INVISIBLE);                  
mPager.setVisibility(View.VISIBLE);


 /*====this code outside OnClickListener Works   ====*/
      mProfileViewPagerAdapter = new ProfileViewPagerAdapter(mContext, cardsModel.getCreatorGallery());                    



 mPager.setAdapter(mProfileViewPagerAdapter);

    /*=======  ======*/

            }
        });

   }


  private class ViewAdapter extends PagerAdapter{
        private Context mContext;
        private List<String> mListData= new ArrayList<String>();


        public ViewAdapter(Context context, List<String> listData){
            mContext = context;
            mListData = listData;
            notifyDataSetChanged();    
        }

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

        @Override
        public int getCount() {
            Log.d(tag, Integer.toString(mListData.size()));
            return mListData.size();
        }

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

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, final int position) {

            LayoutInflater inflater = LayoutInflater.from(mContext);
          ViewGroup view = (ViewGroup) inflater.inflate(R.layout.view_item, container, false);
           final ImageView imageView = view.findViewById(R.id.theImg);
            Log.d(tag, Integer.toString(mListData.size()));

          Glide.with(mContext).load(mListData.get(position)).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).into(imageView);
            container.addView(view);

            return view;
        }    
    }   
}

ArrayAdapter的布局:

cards.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

 <ImageView
    android:layout_width="120dp"
    android:layout_height="120dp"
    android:src="@drawable/empty_profile"
    android:id="@+id/clickImg"/>
     <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="400dp"
           android:visibility="invisible">
        </android.support.v4.view.ViewPager>

</RelativeLayout>

编辑: 我也尝试过:

   mImageView.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {     

             new Handler().post(new Runnable() {
                @Override
                public void run() {
                   mCircleImageView.setVisibility(View.INVISIBLE);                  
                    mPager.setVisibility(View.VISIBLE);

              mProfileViewPagerAdapter = new ProfileViewPagerAdapter(mContext, cardsModel.getCreatorGallery());      

         mPager.setAdapter(mProfileViewPagerAdapter);

          }
        });

      }
     });

并且还试图从OnClickListener内部给Viewpager充气,如果有任何意义的话:

mImageView.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View view) {     

                 new Handler().post(new Runnable() {
                    @Override
                    public void run() {
                 View v =  LayoutInflater.from(mContext).inflate(R.layout.cards, parent, false);

                  mPager = v.findViewById(R.id.vpag);
                       mCircleImageView.setVisibility(View.INVISIBLE);                  
                        mPager.setVisibility(View.VISIBLE);


                  mProfileViewPagerAdapter = new ProfileViewPagerAdapter(mContext, cardsModel.getCreatorGallery());      

             mPager.setAdapter(mProfileViewPagerAdapter);

              }
            });

          }
         });

0 个答案:

没有答案