2017-06-21 178 views
-1

我想添加一個按鈕和按鈕Clicklistenner。 我找不到在哪裏添加按鈕點擊,當我點擊按鈕時,我想要更改imageview(查看器區域)的透明度並更改文本的位置並顯示它。片段和添加按鈕單擊

public class SlideshowDialogFragment extends DialogFragment { 
    private String TAG = SlideshowDialogFragment.class.getSimpleName(); 
    private ArrayList<Image> images; 
    private ViewPager viewPager; 
    private MyViewPagerAdapter myViewPagerAdapter; 
    private TextView lblCount, lblTitle, lblDate; 
    private int selectedPosition = 0; 
    private AdView mAdView; 
    private Button button; 

    static SlideshowDialogFragment newInstance() { 
     SlideshowDialogFragment f = new SlideshowDialogFragment(); 
     return f; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.fragment_image_slider, container, false); 
     viewPager = (ViewPager) v.findViewById(R.id.viewpager); 
     lblCount = (TextView) v.findViewById(R.id.lbl_count); 
     lblTitle = (TextView) v.findViewById(R.id.title); 
     lblDate = (TextView) v.findViewById(R.id.date); 
     button = (Button) v.findViewById(R.id.button); 

     mAdView = (AdView) v.findViewById(R.id.adView2); 
     AdRequest adRequest = new AdRequest.Builder() 
       .build(); 
     mAdView.loadAd(adRequest); 

     images = (ArrayList<Image>) getArguments().getSerializable("images"); 
     selectedPosition = getArguments().getInt("position"); 

     Log.e(TAG, "position: " + selectedPosition); 
     Log.e(TAG, "images size: " + images.size()); 

     myViewPagerAdapter = new MyViewPagerAdapter(); 
     viewPager.setAdapter(myViewPagerAdapter); 
     viewPager.addOnPageChangeListener(viewPagerPageChangeListener); 

     setCurrentItem(selectedPosition); 

     //Toast start 
     Context context = v.getContext(); 
     CharSequence text = "Hello Click Fragment!"; 
     int duration = Toast.LENGTH_SHORT; 

     Toast toast = Toast.makeText(context, text, duration); 
     toast.show(); 
     //Toast finish 

     return v; 



    } 

    private void setCurrentItem(int position) { 
     viewPager.setCurrentItem(position, false); 
     displayMetaInfo(selectedPosition); 
    } 

    // page change listener 
    ViewPager.OnPageChangeListener viewPagerPageChangeListener = new ViewPager.OnPageChangeListener() { 

     @Override 
     public void onPageSelected(int position) { 
      displayMetaInfo(position); 
     } 

     @Override 
     public void onPageScrolled(int arg0, float arg1, int arg2) { 

     } 

     @Override 
     public void onPageScrollStateChanged(int arg0) { 

     } 
    }; 

    private void displayMetaInfo(int position) { 
     lblCount.setText("< "+ (position + 1) + " of " + images.size()+" >"); 

     Image image = images.get(position); 
     lblTitle.setText(image.getName()); 
     lblDate.setText(image.getTimestamp()); 

    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen); 

    } 


    // adapter 
    public class MyViewPagerAdapter extends PagerAdapter { 

     private LayoutInflater layoutInflater; 

     public MyViewPagerAdapter() { 
     } 

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

      layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View view = layoutInflater.inflate(R.layout.image_fullscreen_preview, container, false); 

      ImageView imageViewPreview = (ImageView) view.findViewById(R.id.image_preview); 

      Image image = images.get(position); 

      Glide.with(getActivity()).load(image.getLarge()) 
        .thumbnail(0.5f) 
        .crossFade() 
        .diskCacheStrategy(DiskCacheStrategy.ALL) 
        .into(imageViewPreview); 

      container.addView(view); 

      return view; 
     } 

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

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


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

佈局XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:ads="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:gravity="bottom"> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/viewpager" 
     android:layout_width="match_parent" 
     android:layout_height="400dp" 
     android:background="@android:color/black" /> 

    <TextView 
     android:id="@+id/lbl_count" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="20dp" 
     android:textColor="@android:color/white" 
     android:textSize="16dp"z 
     android:textStyle="bold" /> 

    <Button 
     android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_below="@+id/viewpager" 
     android:background="@color/colorPrimaryDark" 
     android:padding="10dp"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      <com.google.android.gms.ads.AdView 
       android:id="@+id/adView2" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignParentBottom="true" 
       android:layout_centerHorizontal="true" 
       android:layout_marginTop="15dp" 
       ads:adSize="BANNER" 
       ads:adUnitId="@string/banner_home_footer"> 
     </com.google.android.gms.ads.AdView> 

      <TextView 
       android:id="@+id/title" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_margin="10dp" 
       android:textColor="@android:color/white" 
       android:textSize="16dp" 
       android:textStyle="bold" /> 

      <TextView 
       android:id="@+id/date" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignParentBottom="true" 
       android:layout_alignParentLeft="true" 
       android:layout_margin="10dp" 
       android:textColor="@android:color/white" /> 

     </LinearLayout> 
    </ScrollView> 

</RelativeLayout> 
+0

歡迎來到Stack Overflow!似乎有[比問題似乎需要更多的代碼](// stackoverflow.com/help/mcve)。你能否看到將它修剪一些,以便我們能更好地理解它? –

回答

0

要添加OnClickListener應該在onCreateView功能使用上的按鈕功能setOnClickListener。

+0

感謝您的回答 –