Android 后挡板不起作用
我正在尝试创建一个android wear应用程序,它只有一个活动,其中有一个片段。我希望能够根据按钮单击或其他用户操作切换出片段 我可以很好地替换碎片;但是,当我从屏幕左侧滑动到屏幕右侧时。应用程序关闭。我希望这个滑动像一个后退按钮,而不是退出应用程序 主要活动Android 后挡板不起作用,android,android-fragments,wear-os,Android,Android Fragments,Wear Os,我正在尝试创建一个android wear应用程序,它只有一个活动,其中有一个片段。我希望能够根据按钮单击或其他用户操作切换出片段 我可以很好地替换碎片;但是,当我从屏幕左侧滑动到屏幕右侧时。应用程序关闭。我希望这个滑动像一个后退按钮,而不是退出应用程序 主要活动 import android.support.v4.app.Fragment; import android.os.Bundle; import android.support.v4.app.FragmentActivity; imp
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.wearable.view.WatchViewStub;
public class MainActivity extends FragmentActivity implements FragmentChangeListener {
int fragmentContainerId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
final WatchViewStub stub = (WatchViewStub)findViewById(R.id.watch_view_stub);
// instialize the fragment to the loading page
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub watchViewStub) {
fragmentContainerId = R.id.fragmentContainer;
if (watchViewStub.findViewById(R.id.fragmentContainer) != null) {
Fragment1 fragment1= new Fragment1 ();
// In case this activity was started with special instructions from an Intent,
// pass the Intent's extras to the fragment as arguments
fragment1.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container'
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.fragmentContainer, fragment1).addToBackStack(null);
fragmentTransaction.commit();
}
}
});
}
@Override
public void replaceFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragmentContainer, fragment, fragment.toString()).addToBackStack(null);
System.out.println("Entry count in backstack is: " + fragmentManager.getBackStackEntryCount());
fragmentTransaction.commit();
}
}
片段1
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.Button;
public class Fragment1 extends Fragment {
public Fragment1() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment1,
container, false);
Button button1= (Button)view.findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment2 fragment2= new Fragment2 ();
FragmentChangeListener fc=(FragmentChangeListener)getActivity();
fc.replaceFragment(fragment2);
}
});
return view;
}
}
片段2
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.Button;
/**
* A simple {@link Fragment} subclass.
*/
public class Fragment2 extends Fragment {
public Fragment2() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v =inflater.inflate(R.layout.fragment2, container, false);
return v;
}
}
FragmentChangeListner
import android.support.v4.app.Fragment;
public interface FragmentChangeListener
{
public void replaceFragment(Fragment fragment);
}
当我通过按钮从Fragment1导航到Fragment2时,单击。如果不退出应用程序,我如何导航到碎片1? ,如果你考虑使用类,那么你可以使用一个潜在的棘手的解决方案。它允许您向后滑动以查看以前的内容。您必须在滑动时刷新视图以模拟动态后退,但这是可以做到的。一个解决方案是将第二个布局包装在
android.support.wearable.view.SwipedSmissFrameLayout
然后,您可以在父活动调用getFragmentManager().popbackbackstack()中捕获dismise手势代码>
您可以在SwipedSmissFrameLayout上设置侦听器,如下所示:
swipeFrame.setDismissEnabled(true);
swipeFrame.setOnDismissedListener(新的SwipeDismissLayout.OnDismissedListener(){
@凌驾
公共空间布局(SwipedSmissLayout SwipedSmissLayout){
getActivity().getFragmentManager().popBackStack();
}
});
您找到解决方案了吗?是否可以在被刷走的片段后面显示上一个片段?就像它是如何与活动一起工作的。