我正在使用自定义列表视图,在运行应用程序后,一切正常,但在滚动列表时,视图变得混乱。我已粘贴屏幕截图,然后是下面的代码。
我该如何解决这个问题?
初始屏幕:
滚动之后和之后:
MyList.java:
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Typeface;
import android.media.MediaPlayer;
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.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MyList extends Fragment {
ListView list;
MediaPlayer mp;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedinstanceState)
{
mp=MediaPlayer.create(getActivity(), R.raw.wave1);
return inflater.inflate(R.layout.mylistview, container, false);
}
@Override
public void onStart() {
super.onStart();
list=(ListView)getView().findViewById(R.id.listView1);
list.setAdapter(new MyListAdapter(getActivity()));
list.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3)
{
switch(position)
{
case 0:
mp.release();
mp=MediaPlayer.create(getActivity(), R.raw.wave1);
mp.start();
break;
case 1:
mp.release();
mp=MediaPlayer.create(getActivity(), R.raw.wave2);
mp.start();
break;
case 2:
mp.release();
mp=MediaPlayer.create(getActivity(), R.raw.wave3);
mp.start();
break;
case 3:
mp.release();
mp=MediaPlayer.create(getActivity(), R.raw.wave4);
mp.start();
break;
case 4:
mp.release();
mp=MediaPlayer.create(getActivity(), R.raw.wave5);
mp.start();
break;
}
}
});
}
}
class MyListAdapter extends BaseAdapter
{
ArrayList<SingleRow> list;
Context context;
MyListAdapter(Context c)
{
context=c;
list=new ArrayList<SingleRow>();
// Resources res=c.getResources();
// String[] text=res.getStringArray(R.array.text);
int[] images={R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
};
for(int i=0;i<19;i++)
{
list.add(new SingleRow(images[i]));
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row1=inflater.inflate(R.layout.mylist_rows, parent, false);
TextView text=(TextView)row1.findViewById(R.id.textView1);
ImageView image=(ImageView)row1.findViewById(R.id.imageView1);
SingleRow temp=list.get(position);
text.setText(temp.description);
image.setImageResource(temp.image);
Typeface font = Typeface.createFromAsset(text.getContext().getAssets(), "fonts/OpenSans.ttf");
text.setTypeface(font);
return row1;
}
class SingleRow
{
String description;
int image;
SingleRow( int image)
{
//this.description=description;
this.image=image;
}
}
}
MainActivity.java:
import java.util.ArrayList;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
public class MainActivity extends FragmentActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
// nav drawer title
private CharSequence mDrawerTitle;
// used to store app title
private CharSequence mTitle;
// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
// load slide menu items
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
// nav drawer icons from resources
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
navDrawerItems = new ArrayList<NavDrawerItem>();
// adding nav drawer items to array
// Home
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
// Find People
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
// Photos
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
// Communities, Will add a counter here
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1), true, "22"));
// Pages
navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
// What's hot, We will add a counter here
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));
// Recycle the typed array
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
// setting the nav drawer list adapter
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
mDrawerList.setAdapter(adapter);
// enabling action bar app icon and behaving it as toggle button
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_navigation_drawer, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
// on first time display view for first nav item
displayView(0);
}
}
/**
* Slide menu item click listener
* */
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// display view for selected nav drawer item
displayView(position);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/***
* Called when invalidateOptionsMenu() is triggered
*/
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
Fragment fragment = null;
// update the main content by replacing fragments
switch (position) {
case 0:
fragment = new MyList();
break;
}
if (fragment != null) {
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
答案 0 :(得分:0)
更新您的getView()
方法,它会更快地运行。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row1 = null;
if(convertView == null){
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row1=inflater.inflate(R.layout.mylist_rows, parent, false);
}else{
row1 = convertView;
}
TextView text=(TextView)row1.findViewById(R.id.textView1);
ImageView image=(ImageView)row1.findViewById(R.id.imageView1);
SingleRow temp=list.get(position);
text.setText(temp.description);
image.setImageResource(temp.image);
Typeface font = Typeface.createFromAsset(text.getContext().getAssets(), "fonts/OpenSans.ttf");
text.setTypeface(font);
return row1;
}
你也可以优化一些东西:
1如果您的图片始终相同,则可以将代码放入if
阻止
if(convertView == null){
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row1=inflater.inflate(R.layout.mylist_rows, parent, false);
ImageView image=(ImageView)row1.findViewById(R.id.imageView1);
SingleRow temp=list.get(position);
image.setImageResource(temp.image);
}else{
2如果您的字体始终相同,您也可以将其放入if
阻止
if(convertView == null){
// inflating...
TextView text=(TextView)row1.findViewById(R.id.textView1);
Typeface font = Typeface.createFromAsset(text.getContext().getAssets(), "fonts/OpenSans.ttf");
text.setTypeface(font);
}else{
答案 1 :(得分:0)
尝试
list.setCacheColorHint(Color.TRANSPARENT);
答案 2 :(得分:0)
修改你的getView():
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row1 = null;
if(convertView == null){
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row1=inflater.inflate(R.layout.mylist_rows, parent, false);
convertView.setTag(row1);
}else{
row1= (View) convertView.getTag();
}
TextView text=(TextView)row1.findViewById(R.id.textView1);
ImageView image=(ImageView)row1.findViewById(R.id.imageView1);
SingleRow temp=list.get(position);
text.setText(temp.description);
image.setImageResource(temp.image);
Typeface font = Typeface.createFromAsset(text.getContext().getAssets(), "fonts/OpenSans.ttf");
text.setTypeface(font);
return row1;
}
让我知道它是否会起作用。
答案 3 :(得分:0)
解决。事实证明我有两个片段声明。 activitymain.xml中的一个 标记声明和MainActivity.java中的片段事务。根据{{3}} ,您可以在其中任何一个中声明片段,但不能同时声明两者。这就是我获得双重观点的原因。