我们已经让它显示图像并在需要时隐藏它们但是当我向下或向上滚动时它们会消失。谁都看过这个或知道它的原因?不知道最新情况如何,但当我滚动它时好像它是否需要重新确定它是否需要显示并且它没有,即使在那个日期它是肯定的。
public class HistoryFragment extends Fragment {
ListView listTimeline;
SimpleCursorAdapter adapter;
TextView txtCreatedAt, txtFertile, txtTemp, txtCervix;
ImageView imgIntercorse, imgEnergy, imgPregancy, imgMood, imgHeadache, imgPeriod;
IntentFilter filter;
static final String[] FROM = { StatusData.KEY_CHARTING_DATE, StatusData.KEY_CHARTING_NOTES, StatusData.KEY_CHARTING_FERTILE,
StatusData.KEY_CHARTING_TEMPERATURE, StatusData.KEY_CHARTING_PERIOD, StatusData.KEY_CHARTING_INTERCORSE,
StatusData.KEY_CHARTING_MOOD, StatusData.KEY_CHARTING_HEADACHE, StatusData.KEY_CHARTING_TEST,
StatusData.KEY_CHARTING_ENERGY };
static final int[] TO = { R.id.txtCreatedAt, R.id.txtNote, R.id.txtFertile,
R.id.txtTemp, R.id.imgPeriod, R.id.imgIntercorse,
R.id.imgMood, R.id.imgHeadache, R.id.imgPregancy,
R.id.imgEnergy};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_history, container, false);
listTimeline = (ListView) view.findViewById(R.id.listTimeline);
return view;
}
@Override
public void onResume() {
super.onResume();
this.setupList();
}
@Override
public void onPause() {
super.onPause();
}
private void setupList() {
txtCreatedAt = (TextView) getActivity().findViewById(R.id.txtCreatedAt);
txtFertile = (TextView) getActivity().findViewById(R.id.txtFertile);
txtTemp = (TextView) getActivity().findViewById(R.id.txtTemp);
txtCervix = (TextView) getActivity().findViewById(R.id.txtCervix);
imgIntercorse = (ImageView) getActivity().findViewById(R.id.imgIntercorse);
imgEnergy = (ImageView) getActivity().findViewById(R.id.imgEnergy);
imgPregancy = (ImageView) getActivity().findViewById(R.id.imgPregancy);
imgMood = (ImageView) getActivity().findViewById(R.id.imgMood);
imgHeadache = (ImageView) getActivity().findViewById(R.id.imgHeadache);
imgPeriod = (ImageView) getActivity().findViewById(R.id.imgPeriod);
// Get the data
Cursor c = getActivity().getContentResolver().query(StatusProvider.CONTENT_URI_CHARTING, null, null , null, StatusData.KEY_CHARTING_DATE + " DESC"); // <3>
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
adapter = new SimpleCursorAdapter(getActivity(), R.layout.history_row, c, FROM, TO);
adapter.setViewBinder(new CustomViewBinder());
// Assign adapter to ListView
listTimeline.setAdapter(adapter);
}
private class CustomViewBinder implements ViewBinder {
private Date parseDate(String date) {
SimpleDateFormat curFormater = new SimpleDateFormat("yyyy-MM-dd");
Date dateObj = new Date();
try {
dateObj = curFormater.parse(date);
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return dateObj;
}
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_DATE)) {
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
String date = cursor.getString(columnIndex);
Date dateObj = parseDate(date);
String formatedDate = format.format(dateObj);
TextView tv = (TextView) view;
tv.setText(formatedDate);
return true;
}
if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_PERIOD)) {
// If the column is IS_STAR then we use custom view.
String is_period = cursor.getString(columnIndex);
if (is_period != null) {
if (is_period.equalsIgnoreCase("no")){
// set the visibility of the view to GONE
view.setVisibility(View.INVISIBLE);
}
}
return true;
}
if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_INTERCORSE)) {
// If the column is IS_STAR then we use custom view.
String is_intercorse = cursor.getString(columnIndex);
if (is_intercorse != null) {
if (is_intercorse.equalsIgnoreCase("no")) {
// set the visibility of the view to GONE
view.setVisibility(View.INVISIBLE);
}
}
return true;
}
if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_MOOD)) {
// If the column is IS_STAR then we use custom view.
String is_mood = cursor.getString(columnIndex);
if (is_mood != null) {
if (is_mood.equalsIgnoreCase("no") ) {
// set the visibility of the view to GONE
view.setVisibility(View.INVISIBLE);
}
}
return true;
}
if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_HEADACHE)) {
// If the column is IS_STAR then we use custom view.
String is_headache = cursor.getString(columnIndex);
if (is_headache != null) {
if (is_headache.equalsIgnoreCase("no")) {
// set the visibility of the view to GONE
view.setVisibility(View.INVISIBLE);
}
}
return true;
}
if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_TEST)) {
// If the column is IS_STAR then we use custom view.
String is_test = cursor.getString(columnIndex);
if (is_test != null) {
if (is_test.equalsIgnoreCase("no")) {
// set the visibility of the view to GONE
view.setVisibility(View.INVISIBLE);
}
}
return true;
}
if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_ENERGY)) {
// If the column is IS_STAR then we use custom view.
String is_energy = cursor.getString(columnIndex);
if (is_energy != null) {
if (is_energy.equalsIgnoreCase("no")) {
// set the visibility of the view to GONE
view.setVisibility(View.INVISIBLE);
}
}
return true;
}
// For others, we simply return false so that the default binding
// happens.
return false;
}
}
}
XML 1:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="@string/title_history"
android:textSize="30sp" />
<!-- <1> -->
<ListView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/listTimeline"
android:background="#6000" />
</LinearLayout>
XML2:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/lblFertile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txtCreatedAt"
android:layout_below="@+id/txtCreatedAt"
android:text="@string/lblFertile"
android:textStyle="bold" />
<TextView
android:id="@+id/lblTemp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/lblFertile"
android:layout_below="@+id/lblFertile"
android:text="@string/lblTemp"
android:textStyle="bold" />
<TextView
android:id="@+id/txtTemp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/lblTemp"
android:layout_alignBottom="@+id/lblTemp"
android:layout_alignLeft="@+id/txtFertile"
android:paddingLeft="5sp"
android:text="@string/NA" />
<TextView
android:id="@+id/txtCervix"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/lblCervix"
android:layout_alignBottom="@+id/lblCervix"
android:layout_toRightOf="@+id/lblCervix"
android:paddingLeft="5sp"
android:text="@string/NA" />
<TextView
android:id="@+id/txtFertile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/lblFertile"
android:layout_alignBottom="@+id/lblFertile"
android:layout_toRightOf="@+id/lblFertile"
android:paddingLeft="5sp"
android:text="@string/NA" />
<TextView
android:id="@+id/txtCreatedAt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="24dp"
android:layout_toRightOf="@+id/image"
android:gravity="left"
android:paddingLeft="20sp"
android:text="@string/defDate" />
<TextView
android:id="@+id/lblCervix"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/txtFertile"
android:layout_marginLeft="24dp"
android:layout_toRightOf="@+id/txtCreatedAt"
android:text="@string/lblCervix"
android:textStyle="bold" />
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/imgIntercorse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/pregnancy_history"
android:layout_marginLeft="70dp"
android:layout_marginTop="10dp" />
<ImageView
android:id="@+id/imgPregancy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/pregnancy_history"
android:layout_marginLeft="14dp"
android:layout_marginTop="10dp" />
<ImageView
android:id="@+id/imgPeriod"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/period_history"
android:layout_marginLeft="14dp"
android:layout_marginTop="10dp" />
<ImageView
android:id="@+id/imgHeadache"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/pregnancy_history"
android:layout_marginLeft="14dp"
android:layout_marginTop="10dp" />
<ImageView
android:id="@+id/imgMood"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/pregnancy_history"
android:layout_marginLeft="14dp"
android:layout_marginTop="10dp" />
<ImageView
android:id="@+id/imgEnergy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/pregnancy_history"
android:layout_marginLeft="14dp"
android:layout_marginTop="10dp" />
</LinearLayout>
<TextView
android:id="@+id/txtNote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/NA"
android:layout_marginTop="10dp" />
</LinearLayout>
答案 0 :(得分:2)
即使在ViewBinder
中,您也需要始终为视图更改提供恢复选项以处理可能的回收行。我不知道你的图像是哪个,所以这里有一个例子:
if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_PERIOD)) {
// If the column is IS_STAR then we use custom view.
String is_period = cursor.getString(columnIndex);
if (is_period != null) {
if (is_period.equalsIgnoreCase("no")){
// set the visibility of the view to GONE
view.setVisibility(View.INVISIBLE);
} else {
// bring the view to its default state which I'm assuming is being visible
view.setVisibility(View.VISIBLE);
}
}
return true;
}