我有一个寻呼机适配器,假设膨胀表示日历的复杂视图.
每年的日历需要大约350毫秒才能充气.
为了提高性能,我想实现ListView回收视图(convertView参数输入getView())的数组适配器中存在的相同机制.
这是getView()来自适配器的电流.
@Override
protected View getView(VerticalViewPager pager, final DateTileGrid currentDataItem, int position)
{
mInflater = LayoutInflater.from(pager.getContext());
// This is were i would like to understand weather is should use a recycled view or create a new one.
View datesGridView = mInflater.inflate(R.layout.fragment_dates_grid_page, pager, false);
DateTileGridView datesGrid = (DateTileGridView) datesGridView.findViewById(R.id.datesGridMainGrid);
TextView yearTitle = (TextView) datesGridView.findViewById(R.id.datesGridYearTextView);
yearTitle.setText(currentDataItem.getCurrentYear() + "");
DateTileView[] tiles = datesGrid.getTiles();
for (int i = 0; i < 12; i++)
{ …Run Code Online (Sandbox Code Playgroud) 我有Listview与editext和textview.
当我触摸edittext时,edittext失去焦点!
我通过设置解决了这个问题android:windowSoftInputMode="adjustPan"(AndroidManifest.xml).现在我触摸edittext而不是editext获得焦点但是应用程序标签和listview的一些原始消失(顶部).
当用户触摸edittext而不丢失应用程序标签和一些原始列表视图时,我想获得焦点.
我已实施的代码:
当用户触摸edittext时,下面的编码会得到关注,但当软键盘弹出时,应用程序标签和listview的一些原始消失.我想在用户触摸edittext时获得焦点,而不会丢失应用程序标签和一些原始列表视图.
1)的AndroidManifest.xml
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MyListViewDemoActivity"
android:label="@string/app_name"
android:windowSoftInputMode="adjustPan"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Run Code Online (Sandbox Code Playgroud)
2)raw_layout.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText android:id="@+id/mEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
3)main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView android:id="@+id/mListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
4)MyListViewDemoActivity
public class MyListViewDemoActivity extends Activity {
private ListView mListView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mListView=(ListView)findViewById(R.id.mListView); …Run Code Online (Sandbox Code Playgroud) 我正在使用带有ImageAdapter的Gallery来使用ImageViews加载它,从我的资源中提取图像.我的问题是传递给我的适配器中的getView()方法的convertView始终为null.这意味着每次调用getView()时都会创建一个新的ImageView.这会导致可怕的性能,因为GC不断运行以擦除所有这些已创建的并且不再使用ImageView.
这显然是一个已知的错误:Gallery的视图缓存已被破坏; 从不转换观点..
我的两个首选解决方案是1.处理适配器本身的视图缓存,并处理正确重用它们所需的所有逻辑.或者2.包含我自己的Gallery小部件副本并尝试修复它以便正确返回回收的视图.
我已经开始实现第一选项,但我很快意识到我并不完全知道如何制定该操作背后的所有逻辑.我开始认为选项二可能更容易.
我在这里找到了Gallery小部件的代码:http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.1_r2/android/widget/Gallery.java
我不完全理解它,但我可以看到它正在呼唤
child = mAdapter.getView(position, null, this);
Run Code Online (Sandbox Code Playgroud)
在745行.我(在黑暗中拍摄)猜测这是问题的根源.
有没有人有这个bug的经验.或者,任何人都可以指出我正确的方向,以确定回收器的情况如何工作,以便我可以调整这个小部件以正常工作?或者甚至提出一些我可能会忽略的替代选项.
编辑:我找到的最佳解决方案是一个名为EcoGallery的实现.我可以在网上找到它唯一可以参考的地方就在这里.要使其正常工作,您必须将每个块放在项目中的正确位置.
我根据我的需要改编了Jeff Sharkey的SeparatedListAdapter并得到了这样的结果:
public class SeparatedListAdapter<T> extends BaseAdapter {
@SuppressWarnings("unused")
private final String LOG_TAG = getClass().getSimpleName();
public final static int TYPE_SECTION_HEADER = 0;
public final Map<T, Adapter> sectionAdapters;
public final ArrayAdapter<T> headerAdapter;
public SeparatedListAdapter(ArrayAdapter<T> headerAdapter) {
super();
this.sectionAdapters = new LinkedHashMap<T,Adapter>();
this.headerAdapter = headerAdapter;
}
public void addSection(T section, Adapter adapter) {
this.headerAdapter.add(section);
this.sectionAdapters.put(section, adapter);
}
public void clearSections() {
this.headerAdapter.clear();
this.sectionAdapters.clear();
}
@Override
public int getCount() {
// total together all sections, plus one for each section header
int total …Run Code Online (Sandbox Code Playgroud) 这是我的getView().
我显然在这里做错了,因为我列表中的第一项始终没有显示图片.
这里的问题是the convertview因为如果我不回收它,没有问题.
请问我做错了什么?
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null) //IF I DELETE THIS IF EVERYTHING OK!!!
convertView = inflater.inflate(R.layout.square, null);
ImageView image = (ImageView) convertView.findViewById(R.id.background_image);
if (data.get(position).isFollowed()) {
int approprieteimage = getApproppreiateImage(data.get(position));
Picasso.with(context).load(approprieteimage).centerCrop().error(R.drawable.no_image_available).transform(new TealTransformation(context)).fit().into(image);
} else {
int approprieteimage = getApproppreiateImage(data.get(position));
Picasso.with(context).load(approprieteimage).centerCrop().error(R.drawable.no_image_available).fit().into(image);
}
AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(width / 2, width / 2);
convertView.setLayoutParams(layoutParams);
return convertView;
}
Run Code Online (Sandbox Code Playgroud) 我正在使用ArrayAdapter获取我自己的对象类型列表(只有一种类型),并且我为用户提供了创建更多项目的选项(从而为这些项目创建更多视图).在某些时候,getView发送了一个带有非null"convertView"的新"位置"索引.然后它显示最后一个位置的第一个视图.之后,当滚动视图时,所有内容都会混淆.我假设这意味着我以不应该的方式操纵视图,但我只是看不到.这是一些代码:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v;
PreviewItemHolder holder = null;
// Initialize view if convertview is null
if (convertView == null) {
v = newView(parent, position);
}
// Populate from previously saved holder
else {
// Use previous item if not null
v = convertView;
}
// Populate if the holder is null (newly inflated view) OR
// if current view's holder's flag is true and requires populating
if ((holder == null) || …Run Code Online (Sandbox Code Playgroud) 我正在尝试制作一个具有分隔符的ActionBar微调器.我实现了一个SpinnerAdapter有2个项目视图类型(感谢getViewTypeCount).问题是我convertViews从另一种类型发送了一些.
这是我的SpinnerAdapter:
public abstract class SeparatorSpinnerAdapter implements SpinnerAdapter {
Context mContext;
List<Object> mData;
int mSeparatorLayoutResId, mActionBarItemLayoutResId, mDropDownItemLayoutResId, mTextViewResId;
public static class SpinnerSeparator {
public int separatorTextResId;
public SpinnerSeparator(final int resId) {
separatorTextResId = resId;
}
}
public abstract String getText(int position);
public SeparatorSpinnerAdapter(final Context ctx, final List<Object> data, final int separatorLayoutResId, final int actionBarItemLayoutResId,
final int dropDownItemLayoutResId, final int textViewResId) {
mContext = ctx;
mData = data;
mSeparatorLayoutResId = separatorLayoutResId;
mActionBarItemLayoutResId = actionBarItemLayoutResId;
mDropDownItemLayoutResId …Run Code Online (Sandbox Code Playgroud) 如何xml使用 switch case 将搜索字符串中的两个字符串加粗。我想更改每个 xml 字符串textview,此更改将在单击按钮时进行
String strfistsearch= mylist.get(7).get("data4");
if(strfistsearch.equals(strfistsearch))
{
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View convertView1 = mInflater.inflate(R.layout.first_screen,viewFlow);
TextView txtf = (TextView)convertView1.findViewById(R.id.textView11);
String strfistsearch= mylist.get(7).get("data4");
Log.v("strfistsearch", strfistsearch);
SpannableStringBuilder sb = new SpannableStringBuilder(strfistsearch);
final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD); // Span to make text sb.setSpan(bss, 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make first 4 characters Bold sb.setSpan(new ForegroundColorSpan(Color.RED), 0, 1, 0);
txtf.setText(sb);
}
else if(strfistsearch.equals(strfistsearch))
{
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View convertView1 = mInflater.inflate(R.layout.second_screen,viewFlow);
TextView txtf = (TextView)convertView1.findViewById(R.id.textView11); …Run Code Online (Sandbox Code Playgroud) 我开发了一个Android应用程序,其中图像和文本显示在网格视图中,当用户向下滚动时,显示下十个项目(图像和文本).在adapter.notifyDataSetChanged()调用之后调用适配器的getView方法时出现问题.适配器回收数据,但位置重新排列并在网格视图中重复.直到我添加条件来检查convertView是否为null,我才遇到这个问题.
活动类:
public class DynamicListViewActivity extends Activity implements
OnScrollListener {
int visibleElements;
int scrollState;
int count;
TextAdapter adapter = new TextAdapter();
int total=200;// total items limit in grid view
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid);
count=10;
GridView grid = (GridView) findViewById(R.id.gridview);
grid.setAdapter(adapter);
grid.setOnScrollListener(this);
}
public void onScroll(AbsListView view, int firstVisible, int visibleCount,
int totalCount) {
visibleElements= visibleCount;
}
public void onScrollStateChanged(AbsListView v, int s) {
Log.d("ScrollState", s+"");
scrollState=s;
isScrollStateComplete();
}
public void isScrollStateComplete(){
if(visibleElements>0 && scrollState==SCROLL_STATE_IDLE && total>count){ …Run Code Online (Sandbox Code Playgroud) 我正在根据http://bartinger.at/listview-with-sectionsseparators/中描述的技术构建一个带有节的ListView .但是我想通过将convertView重用于非节项目来扩展它.但是,每次输入getView()方法时,我发现convertView变量为null.有人可以解释为什么会这样吗?
ViewHolder holder;
final ListViewItem item = items.get(position);
if (item.isSection()) {
Section section = (Section)item;
convertView = inflater.inflate(R.layout.section, null);
TextView title = (TextView) convertView.findViewById(R.id.section_title);
title.setText(section.title);
} else {
if (convertView == null) {
Log.d("Adapter", "convertView was null");
}
Server server = (Server)item;
convertView = inflater.inflate(R.layout.server_row, null);
holder = new ViewHolder();
holder.serverName = (TextView) convertView.findViewById(R.id.server_name);
holder.serverStatusIcon = (ImageView)convertView.findViewById(R.id.server_status_icon);
convertView.setTag(holder);
holder.serverName.setText(server.name);
}
return convertView;
Run Code Online (Sandbox Code Playgroud)
正在创建和显示列表而没有错误,并且包含部分和非部分项目就好了.
我有一个两难的使用方法
if (convertview==null){
(my code)
}
Run Code Online (Sandbox Code Playgroud)
或不.没有这段代码,我的listview不是很快,它有时会锁定几毫秒,你可以很容易地注意到它在使用中.它无法正常工作.
但是当这段代码时,我的listitems会在一段时间后开始重新计算(10左右)并且我的列表中有一些返回的listitems(使用了我使用的标题).我使用本教程来获取带有部分链接的 listview .列表的长度很好.
当然,我的列表完全没用,有一个重复项目的视图(很好地按路划分),但我也不希望它很慢.有谁知道该怎么办?以下是我的适配器:
公共类DelftAdapter扩展BaseAdapter {
private Activity activity;
private List<ListItem> listItems;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
private final int[] bgColors = new int[] { R.color.list_odd, R.color.list_even };
public DelftAdapter(Activity a, ArrayList<ListItem> li) {
activity = a;
listItems = li;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return listItems.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public …Run Code Online (Sandbox Code Playgroud) 什么是关于if(converView == null){} else {}如果我避免编写else部分以及它如何影响我的代码怎么办?我只是想知道它是否正常工作而没有其他部分.有人可以解释一下convertview的gettag和settag ???
public View getView(int position, View convertView, ViewGroup parent) {
DeviceViewHolder holder = null;
mSelectedDeviceBin = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.single_device_view, null);
holder = new DeviceViewHolder();
holder.deviceId = (TextView) convertView
.findViewById(R.id.deviceId);
holder.deviceType = (TextView) convertView
.findViewById(R.id.deviceType);
convertView.setTag(holder);
} else
holder = (DeviceViewHolder) convertView.getTag();
// Populating the views
holder.deviceId.setText(mSelectedDeviceBin.getDeviceBinId());
StringBuilder deviceCount = new StringBuilder();
deviceCount.append("");
double count = mSelectedDeviceBin.getQtyStock();
deviceCount.append(count);
String deviceCountString = deviceCount.toString();
holder.deviceType.setText("Total number …Run Code Online (Sandbox Code Playgroud) android ×12
convertview ×12
listview ×5
baseadapter ×2
adapter ×1
gallery ×1
gridview ×1
listviewitem ×1
performance ×1
picasso ×1
view ×1
widget ×1