在viewPage上更改TextView时,TextView值永远不会更新。对它的引用解析或似乎但是在将可见性设置为false或使用setText后,我看不到视图中的更改。
我可能会发现这一切都是错误的。感谢任何帮助。
Main.java
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.AlertDialog;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.DialogInterface;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class Main extends FragmentActivity implements TabListener
{
RelativeLayout layout_joystick;
RelativeLayout layout_left_right_joy;
ImageView image_joystick, image_border;
TextView textView1, textView2, textView3, textView4, textView5, textView6,textView7,textView8,textView9,textView10;
TextView debugLabel;
Boolean debug;
JoyStickClass js;
RotationStickClass js2;
private ViewPager viewPager;
private TabPagerAdapter tabPagerAdapter;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//Removes Title
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ViewGroup vi = (ViewGroup)inflater.inflate(R.layout.first_tab, null, false);
textView1 = (TextView)vi.findViewById(R.id.textView1);
textView2 = (TextView)vi.findViewById(R.id.textView2);
textView3 = (TextView)vi.findViewById(R.id.textView3);
textView4 = (TextView)vi.findViewById(R.id.textView4);
textView5 = (TextView)vi.findViewById(R.id.textView5);
textView6 = (TextView)vi.findViewById(R.id.textView6);
textView7 = (TextView)vi.findViewById(R.id.textView7);
textView8 = (TextView)vi.findViewById(R.id.textView8);
textView9 = (TextView)vi.findViewById(R.id.textView9);
textView10 = (TextView)vi.findViewById(R.id.textView10);
debugLabel = (TextView)findViewById(R.id.debugLabel);
viewPager = (ViewPager) findViewById(R.id.viewPager);
tabPagerAdapter = new TabPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(tabPagerAdapter);
//Fragment vif = tabPagerAdapter.getItem(1);
//View vi2 = vif.getView();
//textView2 = (TextView) vi2.findViewById(R.id.textView2);
//textView2.setText("Does this work");
layout_joystick = (RelativeLayout)findViewById(R.id.layout_joystick);
layout_left_right_joy = (RelativeLayout)findViewById(R.id.layout_joystick2);
js = new JoyStickClass(getApplicationContext(), layout_joystick, R.drawable.image_button);
js.setStickSize(75, 75);
js.setLayoutSize(250,250);
js.setLayoutAlpha(250);
js.setStickAlpha(100);
js.setOffset(30);
js.setMinimumDistance(0);
js2 = new RotationStickClass(getApplicationContext(), layout_left_right_joy, R.drawable.image_button);
js2.setStickSize(75, 75);
js2.setLayoutSize(250, 250);
js2.setLayoutAlpha(250);
js2.setStickAlpha(100);
js2.setOffset(30);
js2.setMinimumDistance(0);
layout_joystick.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View arg0, MotionEvent arg1) {
js.drawStick(arg1);
if(arg1.getAction() == MotionEvent.ACTION_DOWN
|| arg1.getAction() == MotionEvent.ACTION_MOVE) {
textView1.setText("X : " + String.valueOf(js.getX()));
textView2.setText("Y : " + String.valueOf(js.getY()));
textView3.setText("Angle : " + String.valueOf(js.getAngle()));
textView4.setText("Distance : " + String.valueOf(js.getDistance()));
int direction = js.get8Direction();
if(direction == JoyStickClass.STICK_UP) {
textView5.setText("Direction : Up");
} else if(direction == JoyStickClass.STICK_UPRIGHT) {
textView5.setText("Direction : Up Right");
} else if(direction == JoyStickClass.STICK_RIGHT) {
textView5.setText("Direction : Right");
} else if(direction == JoyStickClass.STICK_DOWNRIGHT) {
textView5.setText("Direction : Down Right");
} else if(direction == JoyStickClass.STICK_DOWN) {
textView5.setText("Direction : Down");
} else if(direction == JoyStickClass.STICK_DOWNLEFT) {
textView5.setText("Direction : Down Left");
} else if(direction == JoyStickClass.STICK_LEFT) {
textView5.setText("Direction : Left");
} else if(direction == JoyStickClass.STICK_UPLEFT) {
textView5.setText("Direction : Up Left");
} else if(direction == JoyStickClass.STICK_NONE) {
textView5.setText("Direction : Center");
}
} else if(arg1.getAction() == MotionEvent.ACTION_UP) {
textView1.setText("X :");
textView2.setText("Y :");
textView3.setText("Angle :");
textView4.setText("Distance :");
textView5.setText("Direction :");
}
tabPagerAdapter.notifyDataSetChanged();
return true;
}
});
layout_left_right_joy.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View arg0, MotionEvent arg1) {
js2.drawStick(arg1);
if(arg1.getAction() == MotionEvent.ACTION_DOWN
|| arg1.getAction() == MotionEvent.ACTION_MOVE) {
textView6.setText("X : " + String.valueOf(js2.getX()));
textView7.setText("Y : " + String.valueOf(js2.getY()));
textView8.setText("Angle : " + String.valueOf(js2.getAngle()));
textView9.setText("Distance : " + String.valueOf(js2.getDistance()));
int direction = js2.get8Direction();
if(direction == JoyStickClass.STICK_UP) {
textView10.setText("Direction : Up");
} else if(direction == JoyStickClass.STICK_UPRIGHT) {
textView10.setText("Direction : Up Right");
} else if(direction == JoyStickClass.STICK_RIGHT) {
textView10.setText("Direction : Right");
} else if(direction == JoyStickClass.STICK_DOWNRIGHT) {
textView10.setText("Direction : Down Right");
} else if(direction == JoyStickClass.STICK_DOWN) {
textView10.setText("Direction : Down");
} else if(direction == JoyStickClass.STICK_DOWNLEFT) {
textView10.setText("Direction : Down Left");
} else if(direction == JoyStickClass.STICK_LEFT) {
textView10.setText("Direction : Left");
} else if(direction == JoyStickClass.STICK_UPLEFT) {
textView10.setText("Direction : Up Left");
} else if(direction == JoyStickClass.STICK_NONE) {
textView10.setText("Direction : Center");
}
} else if(arg1.getAction() == MotionEvent.ACTION_UP) {
textView6.setText("X :");
textView7.setText("Y :");
textView8.setText("Angle :");
textView9.setText("Distance :");
textView10.setText("Direction :");
}
tabPagerAdapter.notifyDataSetChanged();
return true;
}
});
ImageView imgr2d2 = (ImageView) findViewById(R.id.r2d2);
imgr2d2.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
try
{
MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.r2d2);
mediaPlayer.start();
} catch (IllegalStateException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
debug=true;
final ImageView imgDebug = (ImageView) findViewById(R.id.debug);
imgDebug.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (debug==true)
{
imgDebug.setImageResource(R.drawable.toggle_off);
debug=false;
textView1.setVisibility(View.GONE);
textView2.setVisibility(View.GONE);
textView3.setVisibility(View.GONE);
textView4.setVisibility(View.GONE);
textView5.setVisibility(View.GONE);
textView6.setVisibility(View.GONE);
textView7.setVisibility(View.GONE);
textView8.setVisibility(View.GONE);
textView9.setVisibility(View.GONE);
textView10.setVisibility(View.GONE);
}
else
{
imgDebug.setImageResource(R.drawable.toggle_on);
textView1.setVisibility(View.VISIBLE);
textView2.setVisibility(View.VISIBLE);
textView3.setVisibility(View.VISIBLE);
textView4.setVisibility(View.VISIBLE);
textView5.setVisibility(View.VISIBLE);
textView6.setVisibility(View.VISIBLE);
textView7.setVisibility(View.VISIBLE);
textView8.setVisibility(View.VISIBLE);
textView9.setVisibility(View.VISIBLE);
textView10.setVisibility(View.VISIBLE);
debug=true;
}
tabPagerAdapter.notifyDataSetChanged();
}
});
}
debug=true;
final ImageView imgDebug = (ImageView) findViewById(R.id.debug);
imgDebug.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (debug==true)
{
imgDebug.setImageResource(R.drawable.toggle_off);
debug=false;
textView1.setVisibility(View.GONE);
textView2.setVisibility(View.GONE);
textView3.setVisibility(View.GONE);
textView4.setVisibility(View.GONE);
textView5.setVisibility(View.GONE);
textView6.setVisibility(View.GONE);
textView7.setVisibility(View.GONE);
textView8.setVisibility(View.GONE);
textView9.setVisibility(View.GONE);
textView10.setVisibility(View.GONE);
}
else
{
imgDebug.setImageResource(R.drawable.toggle_on);
textView1.setVisibility(View.VISIBLE);
textView2.setVisibility(View.VISIBLE);
textView3.setVisibility(View.VISIBLE);
textView4.setVisibility(View.VISIBLE);
textView5.setVisibility(View.VISIBLE);
textView6.setVisibility(View.VISIBLE);
textView7.setVisibility(View.VISIBLE);
textView8.setVisibility(View.VISIBLE);
textView9.setVisibility(View.VISIBLE);
textView10.setVisibility(View.VISIBLE);
debug=true;
}
tabPagerAdapter.notifyDataSetChanged();
}
});
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
TabPagerAdapter.java
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class TabPagerAdapter extends FragmentPagerAdapter
{
private final List<Fragment> fragmentList;
public TabPagerAdapter(FragmentManager fm)
{
super(fm);
fragmentList = new ArrayList<Fragment>();
fragmentList.add(new FirstTab());
fragmentList.add(new SecondTab());
fragmentList.add(new ThirdTab());
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getItemPosition(Object object) {
return POSITION_UNCHANGED;
}
@Override
public int getCount()
{
return fragmentList.size();
}
}
FirstTab.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FirstTab extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.first_tab, container, false);
return v;
}
}
main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="@color/black"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#00cd00" >
</android.support.v4.view.ViewPager>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp"
android:layout_centerInParent="true"
android:background="@color/black"
android:orientation="vertical" >
<TextView
android:id="@+id/debugLabel"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="Debug"
android:textColor="#444444"
android:textSize="15dp"
android:layout_centerHorizontal="true"
/>
<ImageView
android:id="@+id/debug"
android:layout_width="70dp"
android:layout_height="50dp"
android:src="@drawable/toggle_on"
/>
</LinearLayout>
<RelativeLayout
android:id="@+id/layout_joystick"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginLeft="5dp"
android:background="@drawable/image_button_bg" >
</RelativeLayout>
<ImageView
android:id="@+id/r2d2"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/r2d2"
android:layout_marginBottom="0dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>
<RelativeLayout
android:id="@+id/layout_joystick2"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="5dp"
android:background="@drawable/left_right_joy" >
</RelativeLayout>
</RelativeLayout>
first_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/DebugRelativeLayout"
android:layout_width="match_parent"
android:background="@color/black"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/TextviewLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@color/black"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="X"
android:textColor="#444444"
android:textSize="20dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Y"
android:textColor="#444444"
android:textSize="20dp" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Angle"
android:textColor="#444444"
android:textSize="20dp" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Distance"
android:textColor="#444444"
android:textSize="20dp" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Direction"
android:textColor="#444444"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/TextviewLayout2"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="50dp"
android:layout_alignParentTop="true"
android:background="@color/black"
android:orientation="vertical" >
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="X"
android:textColor="#444444"
android:textSize="20dp" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Y"
android:textColor="#444444"
android:textSize="20dp" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Angle"
android:textColor="#444444"
android:textSize="20dp" />
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Distance"
android:textColor="#444444"
android:textSize="20dp" />
<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Direction"
android:textColor="#444444"
android:textSize="20dp" />
</LinearLayout>
</RelativeLayout>
答案 0 :(得分:0)
我在这里看到三个问题:
修改TextView
后,您永远不会调用tabPagerAdapter.notifyDataSetChanged()
,这会导致适配器刷新。
在适配器的getItem()
方法中,每次调用方法时都会重新创建片段。娱乐意味着碎片以其原始状态出现。通常,您希望避免在ListView
中进行娱乐。因此,要么将带有片段的列表传递给适配器,要么在其构造函数中创建此列表。
如果我没有记错,则invalidate()
上的ViewGroup
没有必要。
您应该使用适配器。也许这个基本实现会对你有帮助。
public class TabPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> fragmentList;
public FragmentsPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
fragmentList = new ArrayList<>();
fragmentList.add(new FirstTab());
fragmentList.add(new SecondTab());
fragmentList.add(new ThirdTab());
}
@Override
public int getCount() {
return fragmentList.size();
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getItemPosition(Object object) {
return POSITION_UNCHANGED;
}
}