在Viewpager中更改TextView的文本

时间:2016-11-10 20:15:40

标签: android android-fragments android-viewpager textview

我最近开始使用一个小型学校项目编写Android代码。为此,我已经有了一个包含七个碎片的Viewpager。这些片段包括每个TextView。当我点击按钮时,我希望这些TextView立即更改其文本。

为此,我有一个用于更改文本的ToggleButton。我已经尝试过的是在片段中有一个公共'setText'方法,它将TextView的文本设置为另一个(该方法在MainActivity中用String调用)。 但是我收到此错误:无法从静态上下文引用非静态方法setText(java.lang.String)。

所以,我的问题是:如何一次更改七个TextView的文本?

这是我的代码:

MainActivity:

package app.voet.kalenderversemitseneca;

import android.content.Intent;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.widget.ToggleButton;


import java.util.ArrayList;
import java.util.List;

import app.voet.kalenderversemitseneca.Fragments.FiveFragment;
import app.voet.kalenderversemitseneca.Fragments.FourFragment;
import app.voet.kalenderversemitseneca.Fragments.SevenFragment;
import app.voet.kalenderversemitseneca.Fragments.SixFragment;
import app.voet.kalenderversemitseneca.Fragments.ThreeFragment;
import app.voet.kalenderversemitseneca.Fragments.OneFragment;
import app.voet.kalenderversemitseneca.Fragments.TwoFragment;

public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
private Toolbar toolbar;
private ToggleButton toggleButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(false);

    toggleButton = (ToggleButton) findViewById(R.id.toggleButton);

}


private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new OneFragment(), "ONE");
    adapter.addFragment(new TwoFragment(), "TWO");
    adapter.addFragment(new ThreeFragment(), "THREE");
    adapter.addFragment(new FourFragment(), "FOUR");
    adapter.addFragment(new FiveFragment(), "FIVE");
    adapter.addFragment(new SixFragment(), "SIX");
    adapter.addFragment(new SevenFragment(), "SEVEN");
    viewPager.setAdapter(adapter);
}
public void showAboutProjectView(View view){
    Intent intent = new Intent(this, aboutProjectActivity.class);
    startActivity(intent);
}

public void onToggleClicked(View view) {
    // Is the toggle on?
    boolean on = toggleButton.isChecked();
    setText(on);

}
public void setText(boolean translationOn){
    if (translationOn){
        Resources res = getResources();
        String[] versOne = res.getStringArray(R.array.verse_1);
        String[] versTwo = res.getStringArray(R.array.verse_2);
    }
    else {
        Resources res = getResources();
        String[] versOne = res.getStringArray(R.array.verse_1);
        String[] versTwo = res.getStringArray(R.array.verse_2);
        OneFragment.setText(versOne[0]); //here is ERROR
    }
}

}

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

    public ViewPagerAdapter(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);
    }
}

OneFragment(其他片段看起来一样):

package app.voet.kalenderversemitseneca.Fragments;

import android.content.res.Resources;
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.TextView;

import app.voet.kalenderversemitseneca.R;


public class OneFragment extends Fragment{
    private TextView textView1;
    public OneFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    //return inflater.inflate(R.layout.fragment_one, container, false);
    View view = inflater.inflate(R.layout.fragment_one, null);
    textView1 = (TextView)view.findViewById(R.id.TV1);
    return view;
}

public void setText(String text) {
    textView1.setText(text);
}
}

fragment_one.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="app.voet.kalenderversemitseneca.Fragments.OneFragment">

<TextView
android:id="@+id/TV1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textStyle="bold"
android:layout_centerInParent="true"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:text="Hello" />

</RelativeLayout>

如果您发现其他任何我可以改进的地方,请告诉我,因为我还在开始学习:)

谢谢,

莱昂

3 个答案:

答案 0 :(得分:0)

将此添加到ViewPagerAdapter:

public Fragment getFragment(int i) {
    return mFragmentList.get(i);
}

并将setText()替换为:

public void setText(boolean translationOn){
if (translationOn){
    Resources res = getResources();
    String[] versOne = res.getStringArray(R.array.verse_1);
    String[] versTwo = res.getStringArray(R.array.verse_2);
}
else {
    Resources res = getResources();
    String[] versOne = res.getStringArray(R.array.verse_1);
    String[] versTwo = res.getStringArray(R.array.verse_2);
    ((OneFragment)adapter.getFragment(0)).setText(versOne[0]); 
 }
}

并将setupViewPager替换为:

ViewPagerAdapter adapter;
private void setupViewPager(ViewPager viewPager) {
adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new OneFragment(), "ONE");
adapter.addFragment(new TwoFragment(), "TWO");
adapter.addFragment(new ThreeFragment(), "THREE");
adapter.addFragment(new FourFragment(), "FOUR");
adapter.addFragment(new FiveFragment(), "FIVE");
adapter.addFragment(new SixFragment(), "SIX");
adapter.addFragment(new SevenFragment(), "SEVEN");
viewPager.setAdapter(adapter);

}

答案 1 :(得分:0)

好的,我可能会做的是将字符串放在主要活动中,就像这样...

   public String getVers(){
if (translationOn){
    Resources res = getResources();
    String[] versOne = res.getStringArray(R.array.verse_1);
    String[] versTwo = res.getStringArray(R.array.verse_2);
}else {
    Resources res = getResources();
    String[] versOne = res.getStringArray(R.array.verse_1);
    String[] versTwo = res.getStringArray(R.array.verse_2);
    String vers = versOne[0];
} 
    return vers;
}

然后在你的片段活动中我会添加

        final String vers = ((MainActivity) getActivity()).getVers();
if (vers == null) {
 textView1.setText(vers);
}

您可以将最后4行代码添加到所有片段中,然后在打开时,如果切换开关确实设置了文本,则会显示文本视图。

答案 2 :(得分:0)

您可以使用界面进行活动和片段之间的通信 例如:

public interface ITextLitener{
  public void setText(String text);

}

你片段中的

    public class FR1 extends Fragment implements ITextListener{

@Override
setText(String text)
{
txt.setText(text);

}
}

从活动中调用:

((instance of your fragment)your interface ).setText(your text);

例如:

((Frag1)ITextListener).setText("hi");