我的应用有一个Gridview。单击Gridview单元格,用于打开以清单中的对话框为主题的活动。这很好用。
为了进一步改进应用程序,我想在活动中添加一个viewpager。所以我将SomeClass extends Activity
更改为SomeClass extends Fragment
。我创建了一个容器(extends FragmentActivity
)来保存这个Fragment
,我把这个容器作为清单中的对话框主题:
<activity android:name="com.packagename.SomeContainer" android:theme="@style/Theme.Sherlock.Light.Dialog" />
这不会将Fragment显示为对话框。它显示了我不想要的片段全屏,并且它不会根据内容调整大小,它在活动时会执行。
如何将容器(fragmentactivity)或片段显示为应根据内容调整大小而不显示全屏的对话框?
SomeContainer.java
public class SomeContainer extends SherlockFragmentActivity {
private SomeAdapter mAdapter;
private ViewPager mPager;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
overridePendingTransition(R.anim.pull_in_from_left, R.anim.hold);
setContentView(R.layout.layout_events);
getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Intent intent = getIntent();
String date_string = intent.getStringExtra("date_string");
mAdapter = new SomeAdapter(getSupportFragmentManager(), date_string);
mPager = (ViewPager) findViewById(R.id.eventspager);
mPager.setAdapter(mAdapter);
}
@Override
protected void onPause() {
overridePendingTransition(R.anim.hold, R.anim.pull_out_to_left);
super.onPause();
}
public static class SomeAdapter extends FragmentPagerAdapter {
String date;
FragmentManager fm;
public SomeAdapter(FragmentManager fm, String date) {
super(fm);
this.date = date;
this.fm = fm;
}
@Override
public int getCount() {
return 3;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return SomeFragment.newInstance(date);
case 1:
return SomeFragment1.newInstance(date);
case 2:
return SomeFragment2.newInstance(date);
default:
return null;
}
}
}
}
SomeFragment.java
public class SomeFragment extends DialogFragment {
String date_string;
public static SomeFragment newInstance(String date) {
SomeFragment someFragment = new SomeFragment();
Bundle args = new Bundle();
args.putString("date", date);
someFragment.setArguments(args);
returnsomeFragment;
}
/** Called when the activity is first created. */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.events, container, false);
//getActivity().getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
date_string = getArguments().getString("date");
//do something
return v;
}
}