TextView setVisibility无法在ViewPager Fragment中使用

时间:2016-03-31 05:52:01

标签: android android-fragments android-viewpager

我无法在我的片段中将文本视图的可见性设置为VISIBLE.GONE。

我的片段布局:

<FrameLayout 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"
tools:context="b5.project.medibro.fragments.PrescriptionsFragment">


<ListView
    android:layout_width="match_parent"
    android:id="@+id/prescriptions_list"
    android:layout_height="wrap_content" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_input_add" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/noItems"
    android:textSize="18sp"
    android:textColor="@color/myPrimaryColor"
    android:layout_gravity="center"
    android:text="@string/no_medi_items" />

 </FrameLayout>

我已正确实例化textview但无法调试代码。关于这个问题的任何建议? 我的片段java代码是

public class PrescriptionsFragment extends Fragment {

private static final String TAG = PrescriptionsFragment.class.getSimpleName();
FloatingActionButton fab;
ListView prescriptionsList;
private OnFragmentInteractionListener mListener;
private ArrayList<Medication> medications;
TextView tv;
public PrescriptionsFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_prescriptions, container, false);

    tv = (TextView) v.findViewById(R.id.noItems);
    tv.setVisibility(View.GONE);
    fab = (FloatingActionButton) v.findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(getActivity(), AddPrescription.class));
        }
    });

    medications = new ArrayList<>();
    DatabaseHandler db = new DatabaseHandler(getActivity());
    HashMap<Integer, Medication> hashMap = db.getMedicationDetails();

    if (hashMap.isEmpty()) {
        tv.setVisibility(View.VISIBLE);
    } else {
        Log.d(TAG, "hashmap size: " + hashMap.size());
        tv.setVisibility(View.GONE);

        for (int i = 0; i < hashMap.size(); i++) {
            medications.add(i, hashMap.get(i));
        }
        prescriptionsList = (ListView) v.findViewById(R.id.prescriptions_list);
        prescriptionsList.setAdapter(new MyMedAdapter(getActivity(), "all", medications));
    }
    return v;
}

}

我的logcat片段:

03-31 11:33:03.986: D/Database Handler(13900): selectQuery: SELECT * FROM     medication
03-31 11:33:03.987: D/Database Handler(13900): Fetching: Test1
03-31 11:33:03.987: D/PrescriptionsFragment(13900): hashmap size: 1
03-31 11:33:03.988: D/FeedFragment(13900): onResume

有人能说出这段代码的错误吗?

3 个答案:

答案 0 :(得分:1)

尝试

 prescriptionsList.setEmptyView(tv);

listview

中不显示任何数据

答案 1 :(得分:0)

处设置文字视图的可见性
 public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
}

 @Override
public void onResume() {
    super.onResume();
}

将起作用

答案 2 :(得分:0)

noItems textView从布局文件中删除并 setVisibility(View.VISIBLE); 如果需要,可以通过编程方式进行。

OR

尝试

 public static final int VISIBLE = 0x00000000;

 /**
  * This view is invisible, but it still takes up space for layout purposes.
  * Use with {@link #setVisibility}.
  */
 public static final int INVISIBLE = 0x00000004
 public static final int GONE = 0x00000008;

 tv.setVisibility(GONE );