Android图片滑块文字更改

时间:2015-10-16 06:45:18

标签: java android

我使用this进行了Android图片滑动。它工作正常。但我需要一些定制。我需要文字和按钮贝娄图像和文字应根据图像滑动更改。并且文本和按钮不应该滑动。

我做了什么,我在fragment_page上放了两个文本视图和按钮。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:background="@drawable/background"
android:weightSum="1">

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="wrap_content"
    android:layout_height="217dp"
    android:layout_weight="0.71">
</android.support.v4.view.ViewPager>

<TextView
    style="@style/bigFont"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/textBold"
    android:layout_marginTop="20dp"
    android:textColor="@color/text_color"
    android:layout_centerHorizontal="true"/>

<TextView
    style="@style/smallFont"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:id="@+id/textsmall"
    android:layout_marginTop="20dp"
    android:layout_below="@+id/textBold"
    android:textColor="@color/text_color"
    android:layout_centerHorizontal="true"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="REGISTER NOW"
    android:id="@+id/button2"
    android:background="@drawable/button_design"
    android:textColor="@color/text_color"
    android:padding="@dimen/abc_action_bar_content_inset_material"
    android:layout_below="@+id/textsmall"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="60dp" />

</LinearLayout>

这是我的班级。

package com.example.rocku28.mdt;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class Slider extends FragmentActivity {
static final int NUM_ITEMS = 4;
ImageFragmentPagerAdapter imageFragmentPagerAdapter;
ViewPager viewPager;
TextView boldTxt,smallTxt;
Button regButton;

public static final String[] IMAGE_NAME = {"one", "two", "three", "four"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_pager);
    boldTxt = (TextView) findViewById(R.id.textBold);
    smallTxt = (TextView) findViewById(R.id.textsmall);
    imageFragmentPagerAdapter = new       ImageFragmentPagerAdapter(getSupportFragmentManager());
    viewPager = (ViewPager) findViewById(R.id.pager);
    viewPager.setAdapter(imageFragmentPagerAdapter);

}


public static class ImageFragmentPagerAdapter extends FragmentPagerAdapter {
    public ImageFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
    }

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

    @Override
    public Fragment getItem(int position) {
        SwipeFragment fragment = new SwipeFragment();
        return SwipeFragment.newInstance(position);
    }
}

public static class SwipeFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View swipeView = inflater.inflate(R.layout.swipe_fragment, container, false);
        ImageView imageView = (ImageView) swipeView.findViewById(R.id.imageView);

        Bundle bundle = getArguments();
        int position = bundle.getInt("position");
        String imageFileName = IMAGE_NAME[position];
        int imgResId = getResources().getIdentifier(imageFileName, "drawable", "com.example.rocku28.mdt");
        imageView.setImageResource(imgResId);
        setText(position);
        return swipeView;
    }
    public void setText(int position){
        // here I tried to change text on text view
        switch (position){
            case 0:
                new Slider().boldTxt.setText(R.string.verify_licence);
                new Slider().smallTxt.setText(R.string.take_back_picture);
                break;
            case 1:
              //  boldTxt.setText(R.string.verify_licence);
               // smallTxt.setText(R.string.take_front_picture);
                break;
            case 2:
              //  boldTxt.setText(R.string.dashboard);
               // smallTxt.setText(R.string.choose_option_to_call);
                break;
            case 3:
               // boldTxt.setText(R.string.helpline);
              //  smallTxt.setText(R.string.call_call_center);
                break;
        }
    }

    static SwipeFragment newInstance(int position) {
        SwipeFragment swipeFragment = new SwipeFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("position", position);
        swipeFragment.setArguments(bundle);
        return swipeFragment;
    }


}

}

检查settext方法。在这里我尝试更改文本,但问题是这个上下文是在另一个类中,它是静态的,所以我无法改变它。我怎么能让它工作?

0 个答案:

没有答案