如在,ListView的ELEMENTS可以是片段.我知道您可以将TextView XML分配给ListView以更改其外观,但是您可以将片段添加到ListView中.
例如:我有一个片段.所述片段的XML包含一个ImageView,一些大型TextView和一个小型TextView.Fragment类代码接收Bundle,然后根据内容相应地填充TextViews和ImageView.Fragment XML和Fragment代码都可以正常工作(我可以很好地显示单个Fragment).我有一个FragmentActivity,我想在其中显示上面提到的碎片列表.这是我用来尝试填充FragmentActivity视图中的ListView的代码:
ArrayList<Fragment> fragList = new ArrayList<Fragment>();
Fragment fragment = Fragment.instantiate(this, TileItem.class.getName());
Bundle bundle = new Bundle();
bundle.putInt("key", 0);
fragment.setArguments(bundle);
fragList.add(fragment);
ArrayAdapter<Fragment> adapter = new ArrayAdapter<Fragment>(this, R.layout.tile_item, fragList);
listItems.setAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)
这是我对此的思考方式.我创建了一个Fragments的ArrayList来保存我所有实例化的视图.然后创建一个片段,创建一个包,将数据添加到包(这样的片段可以正确地封送数据到它的意见),加上捆绑的片段,然后最后片段添加到ArrayList.之后,我创建一个ArrayAdapter,添加我想要使用的元素布局,以及我制作的片段列表; 然后将ListView设置为从我的适配器读取.
任何运行此代码的人都可能会获得NPE @实例化ArrayAdapter.是什么赋予了?这甚至可能吗?在我继续绞尽脑汁之前,有人可以告诉我,如果我只是在浪费时间吗?有没有更好的办法?我一直在想用滚动型的,但这么多的ListView的功能将需要重新实现,我讨厌恨,恨重复发明轮子时,它不是必需的.
感谢所有人的阅读,特别感谢您的决定,如果您决定离开他们.我试图寻找一个既定的答案,但我似乎找到的是关于使用片段的ListView INSIDE的问题/网页; 不使用碎片作为ListView的元素
编辑:我接受了以下建议,并开始调查更多.从事情出现,我应该能够使用该膨胀片段,而不是从XML只是平了建筑的自定义接口的方式(因为缺乏一个更好的方式来描述这一过程),但我目前的实现正试图设置时抛出NPE适配器.
这是我的自定义适配器代码(为简洁起见缩短):
public class AdapterItem extends ArrayAdapter<Fragment> {
Context c;
List<Fragment> f;
public AdapterItem(Context c, List<Fragment> f) {
super(c, R.layout.tile_item, f);
this.c = c;
this.f = f;
}
@Override
public View getView(int pos, View v, ViewGroup vg) {
LayoutInflater i = (LayoutInflater) …Run Code Online (Sandbox Code Playgroud) 我的应用程序使用片段活动,它只处于纵向模式,无法旋转屏幕.
最初我使用的是这种commit()方法,但现在我计划不加区别地将这些更改commitAllowingStateLoss()为片段活动
如果不重新评估我使用片段的每个个案,是否有任何理由不得不加区分?
如果我尝试在片段中给视图充气,我会得到NULL.例如:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Here I will inflate my view using the layout ID & return.
return view;
}
Run Code Online (Sandbox Code Playgroud)
每当点击一个按钮时,我需要创建一个动态视图,例如:button&add to LinearLayout.我想在我的片段类中执行此操作,如下所示:
public void addPlaces() {
Button button = new Button(null);
button.setText("button name");
// e.g. like adding button to enter code here linear layout
linearLayout.addView(button);
}
Run Code Online (Sandbox Code Playgroud)
所以,如果我在onCreateView中膨胀LinearLayout并在add class中使用它,我就会得到NULL.怎么实现?
出于某种原因,当我尝试显示一个Dialog时,我从dialog.show(fm,DIALOG_DATE)收到错误; 说无法解析方法'show(android.support.v4.app.FragmentManager,java.lang.String)'
为什么不能解决方法?
mDateButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
FragmentManager fm = getActivity().getSupportFragmentManager();
DatePickerFragment dialog = new DatePickerFragment();
dialog.show(fm, DIALOG_DATE);
}
});
Run Code Online (Sandbox Code Playgroud)
这是我班上的其他人:
package com.bignerdranch.android.criminalintent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import java.util.UUID;
public class CrimeFragment extends Fragment {
//key for the extra
public static final String EXTRA_CRIME_ID = "com.bignerdranch.android.criminalintent.crime_id";
private static final String DIALOG_DATE = "date"; …Run Code Online (Sandbox Code Playgroud) 我的Android应用程序启动到BeginActivity,这是SherlockFragmentActivity的子类,并显示它的第一个视图使用:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
Fragment f = LoginFragment.newInstance();
getSupportFragmentManager()
.beginTransaction()
.add(android.R.id.content, f, "loginfragment")
.attach(f)
.commit();
}
}
Run Code Online (Sandbox Code Playgroud)
LoginFragment显示如下视图:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.login, container, false);
// Get pointers to text views
usernameField = (EditText) v.findViewById(R.id.usernameLog);
passwordField = (EditText) v.findViewById(R.id.passwordLog);
progressBar = (ProgressBar) v.findViewById(R.id.progressBarLog);
// Set button click listeners for both buttons
Button …Run Code Online (Sandbox Code Playgroud) 我是android编程新手android studio中的拖放功能无法正常工作.我无法将任何东西拖到我的手机上.现在我的片段是谷歌地图视图,我想在视图中添加一个搜索栏.它只是不拖.当我尝试在文本中添加它时,它说元素项不允许在这里?
这是我的activity_maps.xml文件
<fragment 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" android:id="@+id/map" tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment">
<item android:id="@+id/search"
android:title="@string/search_title"
android:icon="@drawable/ic_search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
</fragment>
Run Code Online (Sandbox Code Playgroud) android drag-and-drop fragment android-layout android-studio
无论我在哪里,FrameLayout似乎都被用作FragmentContainer.为什么FrameLayout总能看到Fragments?
FragmentTransaction完成后是否可以有一个事件?
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment).commit();
Run Code Online (Sandbox Code Playgroud)
事实上,我为我的应用程序使用抽屉,并且我想invalidateOptionsMenu();在事务完成时触发以更改操作栏的菜单.
我想在片段中添加一个TabHost.代码如下.这里面是碎片.我正在尝试添加TabHost以显示两个选项卡:
package com.nordicsoft.dilosysNewVersion;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
public class Fragment_Coupons extends Fragment {
TabHost tabHost;
TabHost.TabSpec spec;
public Fragment_Coupons() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_coupon, container,
false);
tabHost = (TabHost) rootView.findViewById(android.R.id.tabhost);
tabHost.setup();
//Add_Text_To_Tab("Android", "IOS");
Intent intentAndroid = new Intent().setClass(getActivity(),
Aclass.class);
spec = tabHost.newTabSpec("Android").setContent(intentAndroid)
.setIndicator("Android");
tabHost.addTab(spec);
Intent intentBus = new Intent().setClass(getActivity(), Bclass.class);
spec = tabHost.newTabSpec("Welcome").setIndicator("Welcome")
.setContent(intentBus).setIndicator("Welcome");
tabHost.addTab(spec);
return rootView;
}
} …Run Code Online (Sandbox Code Playgroud) 出于某种原因,非IE浏览器似乎在发送服务器端重定向(使用Location头)时保留URL哈希(如果存在).例:
// a simple redirect using Response.Redirect("http://www.yahoo.com");
Text.aspx
Run Code Online (Sandbox Code Playgroud)
如果我访问:
Test.aspx#foo
Run Code Online (Sandbox Code Playgroud)
在Firefox/Chrome中,我被带到:
http://www.yahoo.com#foo
Run Code Online (Sandbox Code Playgroud)
谁能解释为什么会这样?我已经在不同的平台上尝试了各种服务器端重定向(虽然所有这些都导致了Location头),但这似乎总是发生.我没有在HTTP规范中看到它,但它似乎确实是浏览器本身的问题.URL哈希(如预期的那样)永远不会发送到服务器,因此服务器重定向不会被它污染,浏览器只是出于某种原因持久化它.
有任何想法吗?