我有一个片段,必须是viewPager中的布局之一。我的片段有一个listView,应该填充一些常量文本。 如下所示:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Display_Rows();
}
private void Display_Rows() {
Log.d(TAG, "Displaying Rows...");
final ListView lv =(ListView) rootView.findViewById(R.id.Asset_Scanned_listView);
final List<String> lst = new ArrayList<String>();
lst.add("1");
lst.add("2");
lst.add("3");
lst.add("4");
lst.add("5");
lst.add("6");
lst.add("7");
lst.add("8");
lst.add("9");
lst.add("10");
ArrayAdapter<String> ArAd =new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,lst);
lv.setAdapter(ArAd);
lv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3){
Toast.makeText(getActivity(), String.valueOf(((TextView)arg1).getText()), Toast.LENGTH_SHORT).show();
}});
Log.d(TAG, "Rows Displayed");
}
这是我的xml的一部分:
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/RLayoutSort"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<ListView
android:id="@+id/Asset_Scanned_listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</ScrollView>
但我的listView只显示第一个文本。任何人都可以帮我解决我的代码中出了什么问题吗?
答案 0 :(得分:5)
天才试试这个
<强> MainActivity.java 强>
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* Declare ListView .
*/
ListView lv;
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
/**
* Inititlize.
*/
lv = (ListView) rootView.findViewById(R.id.listView);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
List<String> lst = new ArrayList<String>();
for(int i = 1; i <= 10; i++) {
lst.add(i + "");
}
ArrayAdapter<String> ArAd = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, lst);
lv.setAdapter(ArAd);
}
}
}
<强> fragment_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:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#e5e5e5"
tools:context="com.example.app.MainActivity$PlaceholderFragment" >
<ListView
android:id="@+id/listView"
android:textColor="#FF0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:dividerHeight="1dp"/>
</RelativeLayout>
结果
答案 1 :(得分:2)
设置ScorllView
android:layout_width="match_parent"
由于ListView
的父级不是match_parent
或fill_parent
,因此您的ListView
不会是全宽。
答案 2 :(得分:0)
试试这个
<强> main.xml中强>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:fillViewport="true"
android:gravity="center" >
<ListView
android:id="@+id/Asset_Scanned_listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:cacheColorHint="#374c0e"
android:divider="#004466"
android:dividerHeight="1dp"
android:listSelector="#99ddff" >
</ListView>
</ScrollView>
<强> MainActivity.java 强>
public class MainActivity extends ActionBarActivity {
.......
public static class PlaceholderFragment extends Fragment {
/**
* Declaration.
*/
List<String> lst;
ListView listView;
private String TAG = "MainAcitivity";
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
listView = (ListView) rootView
.findViewById(R.id.Asset_Scanned_listView);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Display_Rows();
}
private void Display_Rows() {
Log.d(TAG, "Displaying Rows...");
List<String> lst = new ArrayList<String>();
lst.add("1");
lst.add("2");
lst.add("3");
lst.add("4");
lst.add("5");
lst.add("6");
lst.add("7");
lst.add("8");
lst.add("9");
lst.add("10");
ArrayAdapter<String> ArAd = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, lst);
listView.setAdapter(ArAd);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(getActivity(),
String.valueOf(((TextView) arg1).getText()),
Toast.LENGTH_SHORT).show();
}
});
Log.d(TAG, "Rows Displayed");
}
}
}
结果