来自Fragment

时间:2015-11-30 14:15:36

标签: android android-fragments android-xml

美好的一天!我有1个MainActivity和几个片段。 MainActivity的布局有3个ImageButtons,我想从Fragment改变它们的背景:所以我有ViewPager,滑动图片工作正常,在图片下面我有这些ImageButtons。他们的背景基于现在的图片。图片改变得很好,但按钮背景随机变化(显然它有一些逻辑,但我无法理解)。所以问题在于ImageButtons,它们的背景没有正确改变。

PageFragment:

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.ImageButton;
import android.widget.ImageView;

public class PageFragment extends Fragment {

    static final String ARGUMENT_PAGE_NUMBER = "arg_page_number";

    int pageNumber;

    public static PageFragment newInstance(int page) {
        PageFragment pageFragment = new PageFragment();
        Bundle arguments = new Bundle();
        arguments.putInt(ARGUMENT_PAGE_NUMBER, page);
        pageFragment.setArguments(arguments);
        return pageFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pageNumber = getArguments().getInt(ARGUMENT_PAGE_NUMBER);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.sliding_fragment, null);

        ImageView ivPicture = (ImageView) view.findViewById(R.id.ivPic);
        ImageButton btn1 = (ImageButton) getActivity().findViewById(R.id.imgBtn1);
        ImageButton btn2 = (ImageButton) getActivity().findViewById(R.id.imgBtn2);
        ImageButton btn3 = (ImageButton) getActivity().findViewById(R.id.imgBtn3);

        switch (pageNumber) {
            case 0:
                ivPicture.setBackgroundResource(R.drawable.flowers_no_back1);
                btn1.setBackgroundResource(R.drawable.ic_radio_button_on_24dp);
                btn2.setBackgroundResource(R.drawable.ic_radio_button_off_24dp);
                btn3.setBackgroundResource(R.drawable.ic_radio_button_off_24dp);
                break;
            case 1:
                ivPicture.setBackgroundResource(R.drawable.flowers_no_back2);
                btn1.setBackgroundResource(R.drawable.ic_radio_button_off_24dp);
                btn2.setBackgroundResource(R.drawable.ic_radio_button_on_24dp);
                btn3.setBackgroundResource(R.drawable.ic_radio_button_off_24dp);
                break;
            case 2:
                ivPicture.setBackgroundResource(R.drawable.flowers_no_back3);
                btn1.setBackgroundResource(R.drawable.ic_radio_button_off_24dp);
                btn2.setBackgroundResource(R.drawable.ic_radio_button_off_24dp);
                btn3.setBackgroundResource(R.drawable.ic_radio_button_on_24dp);
                break;
            default:
                break;
        }

        return view;
    }
}

PageFragment的布局(sliding_fragment.xml):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/ivPic"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"/>

</RelativeLayout>

内部类MyFragmentPagerAdapter的MainActivity:

import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    NewsFragment fragmentNews;


    private static final int NUM_PAGES = 3;
    private PagerAdapter pagerAdapter;
    private static final String TAG = "myLogs";

    @Override
    protected void onCreate(Bundle savedInstanceState) {

    ...
        fragmentNews = new NewsFragment();            
        FragmentManager fm = getSupportFragmentManager();
        fm.beginTransaction().add(R.id.container_for_fragments, fragmentNews).commit();

        pagerAdapter = new MyFragmentPagerAdapter(fm);

    }

    public void viewIsCreated() {
        ViewPager pager = fragmentNews.getPager();
        pager.setAdapter(pagerAdapter);                   

    }




    private class MyFragmentPagerAdapter extends FragmentPagerAdapter {

        public MyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return PageFragment.newInstance(position);
        }

        @Override
        public int getCount() {
            return NUM_PAGES;
        }

    }
}

fragment_news.xml(我把ViewPager和按钮放在哪里)。我在类NewsFragment中使用的这个布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="100"
    tools:context=".fragments.NewsFragment">


    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/sliding_pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="30" />


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_centerHorizontal="true"
        android:layout_weight="10">


        <ImageButton
            android:id="@+id/imgBtn1"
            android:layout_width="@dimen/toggle_button_size"
            android:layout_height="@dimen/toggle_button_size"
            android:layout_centerVertical="true"
            android:layout_marginRight="5dp"
            android:layout_toLeftOf="@+id/imgBtn2"
            android:layout_toStartOf="@+id/imgBtn2"
            android:background="@drawable/ic_radio_button_on_24dp"
            android:clickable="true" />

        <ImageButton
            android:id="@+id/imgBtn2"
            android:layout_width="@dimen/toggle_button_size"
            android:layout_height="@dimen/toggle_button_size"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:background="@drawable/ic_radio_button_off_24dp"
            android:clickable="true" />

        <ImageButton
            android:id="@+id/imgBtn3"
            android:layout_width="@dimen/toggle_button_size"
            android:layout_height="@dimen/toggle_button_size"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5dp"
            android:layout_toEndOf="@+id/imgBtn2"
            android:layout_toRightOf="@+id/imgBtn2"
            android:background="@drawable/ic_radio_button_off_24dp"
            android:clickable="true" />


    </RelativeLayout>
...

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

看看Fragmnet到活动的沟通。 http://simpledeveloper.com/how-to-communicate-between-fragments-and-activities/

使用界面即可实现此目的

答案 1 :(得分:0)

我找到了回答我的问题。因此,您必须将此代码放在viewIsCreated()中的MainActivity.java中:

public void viewIsCreated() {
        ViewPager pager = fragmentNews.getPager();
        pager.setAdapter(pagerAdapter);


        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            ImageButton btn1 = (ImageButton) findViewById(R.id.imgBtn1);
            ImageButton btn2 = (ImageButton) findViewById(R.id.imgBtn2);
            ImageButton btn3 = (ImageButton) findViewById(R.id.imgBtn3);

            @Override
            public void onPageSelected(int position) {
                Log.d(TAG, "onPageSelected, position = " + position);

                switch (position) {
                    case 0:
                btn1.setBackgroundResource(R.drawable.ic_radio_button_on_24dp);
                btn2.setBackgroundResource(R.drawable.ic_radio_button_off_24dp);
                btn3.setBackgroundResource(R.drawable.ic_radio_button_off_24dp);
                        break;
                    case 1:
                btn1.setBackgroundResource(R.drawable.ic_radio_button_off_24dp);
                btn2.setBackgroundResource(R.drawable.ic_radio_button_on_24dp);
                btn3.setBackgroundResource(R.drawable.ic_radio_button_off_24dp);
                        break;
                    case 2:
                btn1.setBackgroundResource(R.drawable.ic_radio_button_off_24dp);
                btn2.setBackgroundResource(R.drawable.ic_radio_button_off_24dp);
                btn3.setBackgroundResource(R.drawable.ic_radio_button_on_24dp);
                        break;
                    default:
                        break;
                }

            }

            @Override
            public void onPageScrolled(int position, float positionOffset,
                                       int positionOffsetPixels) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }

        });

    }