我正在努力学习,目前正试图通过The Big Nerd Ranch指南向我学习一些Android。 但我很难理解我如何让应用程序重新排列“新片段”中的“详细视图”,将listview重叠到sidebyside视图(listview left,details right)。
我现在在网上尝试了很多不同的指南和教程,但我想我只是为了不稳定而真正弄明白了。因为所有这些都是这样或那样的结果只是最终成为零点等等。我猜测这与“书”构建它的方式与我见过的指南有关。所以希望这里有人可以给我一个提示。
下面的代码将显示和listview,当我点击一个项目时,它将用另一个基于点击的id显示该项目的片段替换列表片段。 需要它在横向显示列表视图旁边的信息。
SignsMainActivity.class(首先开始于: Intent i = new Intent(getActivity(),SignsMainActivity.class); startActivity(ⅰ))
public class SignsMainActivity extends SingleFragmentActivity {
@Override
protected Fragment createFragment() {
return new SignsMainFragment(); }}
SignsMainFragment.class
public class SignsMainFragment extends ListFragment {
private ArrayList<Signs> storage;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
storage = SignStorage.get(getActivity()).getStorageOfSigns();
SignAdapter adapter = new SignAdapter(storage);
setListAdapter(adapter);
}
@Override
public void onResume() {
super.onResume();
((SignAdapter)getListAdapter()).notifyDataSetChanged();
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Signs signs = ((SignAdapter)getListAdapter()).getItem(position);
Intent intent = new Intent(getActivity(), SignsActivity.class);
intent.putExtra(SignsFragment.EXTRA_SIGNS_ID, signs.getId());
startActivity(intent);}
private class SignAdapter extends ArrayAdapter<Signs> {
public SignAdapter(ArrayList<Signs> sSigns){
super(getActivity(), 0, sSigns);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
//If we were given a view, inflate one
if(convertView ==null){
convertView = getActivity().getLayoutInflater().inflate(R.layout.sign_row, null);
}
//Configure the view for this Crime
Signs signs = getItem(position);
TextView titleTextView = (TextView)convertView.findViewById(R.id.label);
titleTextView.setText(signs.getName());
return convertView; }}}
SignsActivity.class
public class SignsActivity extends SingleFragmentActivity {
@Override
protected Fragment createFragment() {
UUID signsId = (UUID) getIntent().getSerializableExtra(SignsFragment.EXTRA_SIGNS_ID);
return SignsFragment.newInstance(signsId);}}
SignsFragment.class
public class SignsFragment extends Fragment{
public static final String EXTRA_SIGNS_ID = "signs_id";
private Signs mSign;
private TextView nameURLView;
private TextView imageURLView;
private TextView illustrationURLView;
@Override
public void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
UUID signsID = (UUID)getArguments().getSerializable(EXTRA_SIGNS_ID);
mSign = SignStorage.get(getActivity()).getSign(signsID);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.sign_details,parent,false);
nameURLView = (TextView)view.findViewById(R.id.details);
nameURLView.setText(mSign.getName());
return view;
}
public static SignsFragment newInstance(UUID signsId){
Bundle args = new Bundle();
args.putSerializable(EXTRA_SIGNS_ID, signsId);
SignsFragment fragment = new SignsFragment();
fragment.setArguments(args);
return fragment;
}}
SingleFragmentActivity.class(我猜这里是我需要调整魔法的地方吗?)
public abstract class SingleFragmentActivity extends FragmentActivity {
//Used to instanciate the fragment (subclasses of this class will implement the method to return an instance of the fragment that the activity is hosting)
protected abstract Fragment createFragment();
private FragmentManager fm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_fragment);
fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.singleFragmentContainer);
if(fragment == null){
fragment = createFragment();
fm.beginTransaction().add(R.id.singleFragmentContainer, fragment).commit();
}
}}
存储标志对象:
public class SignStorage {
private static SignStorage mSignStorage;
private Context context;
private ArrayList<Signs> storageOfSigns;
private Signs signs;
private static final String TAG="xx";
private SignStorage(Context appContext){
context = appContext;
storageOfSigns = new ArrayList<Signs>();
primitiveFix();
}
public static SignStorage get(Context c){
if (mSignStorage == null){
mSignStorage = new SignStorage(c.getApplicationContext());
}
return mSignStorage;
}
public ArrayList<Signs> getStorageOfSigns() {
return storageOfSigns;
}
public void setStorageOfSigns(ArrayList<Signs> storageOfSigns) {
this.storageOfSigns = storageOfSigns;
}
public Signs getSign(UUID id){
for (Signs s : storageOfSigns){
if (s.getId().equals(id))
return s;
}
return null;
}
private void primitiveFix(){
String basePath = Environment.getExternalStorageDirectory().toString()+"/PictureTalk/Signs/";
String name;
String imageURL;
String videoUrl;
String illuUrl;
Signs s;
name = "abonnere";
imageURL = basePath+"picture/adresse.jpg";
videoUrl = basePath+"video/adresse.mp4";
illuUrl = basePath+"illustrative/adresse.jpg";
s = new Signs(name,imageURL,videoUrl,illuUrl);
storageOfSigns.add(s);
THIS LIST GOES ON DOWNWARDS to get some items to display, will be filled later in another
way...
Signs.class
public class Signs {
UUID id;
String name;
String videoURL;
String imageURL;
String illustrativeURL;
public Signs(String name,String videoURL,String imageURL,String illustrativeURL){
this.id = UUID.randomUUID();
this.name = name;
this.videoURL = videoURL;
this.imageURL = imageURL;
this.illustrativeURL = illustrativeURL;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getVideoURL() {
return videoURL;
}
public void setVideoURL(String videoURL) {
this.videoURL = videoURL;
}
public String getBitmapURL() {
return imageURL;
}
public void setBitmapURL(String bitmapURL) {
this.imageURL = bitmapURL;
}
public UUID getId() {
return id;
}
}
acitivty_single_fragment.xml(第一个空白xml)
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/singleFragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
sign_row.xml(列表视图的xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="20px" >
</TextView>
</LinearLayout>
sign_details.xml(要显示的对象的详细区域,没有添加更多文本框喷射......)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="20px" >
</TextView>
</LinearLayout>
答案 0 :(得分:0)
要使用片段实现列表/详细信息屏幕,您希望拥有一个包含两个片段的活动,而不是两个单独的活动。通常,您可以将活动视为代表全屏。
换句话说,您不需要SingleFragmentActivity或SignsActivity。然后,您的SignsMainActivity布局XML可以将您的列表和详细信息片段并排放置。片段指南有一个很好的XML示例: http://developer.android.com/guide/components/fragments.html
复制该XML并更改名称会为您提供一个新的signs_main_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="SignsMainFragment"
android:id="@+id/signs-main"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="SignsFragment"
android:id="@+id/signs"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
然后将此XML设置为主要活动的内容视图:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.signs_main_activity);
}