Android ViewPager 不显示任何视图



我在我的线性布局中添加了一个视图寻呼器,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:background="@drawable/rounded_corner_view_bg">
<TextView
android:id="@+id/memberRegion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@drawable/doctor_location" />
<TextView
android:id="@+id/memberLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/availabilityPager"
android:layout_width="match_parent"
android:layout_height="200dp" />
</LinearLayout>

并编写了适配器的代码如下:

package eng.hasan.medico;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import java.sql.Time;
public class AvailabilityPagerAdapter extends PagerAdapter {
int innserSearchedDocIndex;
AvailabilityPagerAdapter(int searchedDocIndex){
innserSearchedDocIndex = searchedDocIndex;
}
@Override
public int getCount() {
return Globals.searchedDocs.get(innserSearchedDocIndex).availabilities.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view.equals(object);
}
@Override
public Object instantiateItem(ViewGroup view, int i) {
LayoutInflater lI = (LayoutInflater) Globals.memberPageGlobalObject.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = lI.inflate(R.layout.availability_item ,view ,true);
TextView availabilityDay = v.findViewById(R.id.availabilityDay);
String[] days = {"فارغ", "السبت", "الأحد", "الاثنين", "الثلاثاء", "الأربعاء", "الخميس", "الجمعة"};
availabilityDay.setText(days[Integer.parseInt(Globals.searchedDocs.get(innserSearchedDocIndex).availabilities.get(i).get(0))]);
TextView availabilityTime = v.findViewById(R.id.availabilityTime);
Time avTimeFrom = Time.valueOf(Globals.searchedDocs.get(innserSearchedDocIndex).availabilities.get(i).get(1));
Time avTimeTo = Time.valueOf(Globals.searchedDocs.get(innserSearchedDocIndex).availabilities.get(i).get(2));
availabilityTime.setText(avTimeFrom.toString() + "n" + avTimeTo);
return v;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}
}

以下是每个页面中视图的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="100dp"
android:layout_height="wrap_content">
<TextView
android:id="@+id/availabilityDay"
android:layout_width="match_parent"
android:layout_height="20dp"
android:textAlignment="center"
android:background="@drawable/left_right_top_rounded_corner"
android:text="السبت" />
<TextView
android:id="@+id/availabilityTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="3 م n إلى n 9 م" />
<Button
android:id="@+id/availabilityReserve"
android:layout_width="match_parent"
android:layout_height="20dp"
android:background="@drawable/left_right_bottom_rounded_corner_red"
android:text="احجز" />
</LinearLayout>

该程序工作正常,没有错误,但视图寻呼机没有显示我尝试使用的任何页面(android.support.v4.view.ViewPager(,该页面过去曾与我一起工作,但新版本的android工作室(3.5(和新的SDK不起作用。我在我的 Gradle 中(minSdkVersion 19 ,targetSdkVersion 29,compileSdkVersion 29(,请告诉我我是否做错了什么?

包含 ListView 内部 ViewView 的活动是 (Java 代码(

package eng.hasan.medico;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import org.json.JSONArray;
import org.json.JSONException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class MemberPage extends AppCompatActivity {
String title = "بيانات الدكتور";
ListView memberList;
TextView innerMemberInfo;
ProgressBar docInfoProgressBar;
ProgressBar headerProgressBar;
ImageView userLikeButton;
RatingBar docRatingBar;
boolean userSignedIn = false;
public boolean docLiked;
public boolean docRated;
public int views ;
public float rate;
boolean RATING_MODE = false;
ViewPager avPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_member_page);
Globals.memberPageGlobalObject = this;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowCustomEnabled(true);
setActivityTitle();
rate = 0;
checkUserLiked();
views = Globals.searchedDocs.get(getIntent().getExtras()
.getInt("docObjIndex")).views;
setMemberList();
/*Toast.makeText(this ,String.valueOf(Globals.searchedDocs.get(getIntent().getExtras()
.getInt("docObjIndex")).availabilities.get(0).size()) ,Toast.LENGTH_LONG).show();*/


////////////////////////////////////////////////////
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
finish();
return super.onOptionsItemSelected(item);
}
private void setMemberList(){
memberList = findViewById(R.id.memberList);
memberList.setAdapter(new MemberListArrayAdapter(getIntent().getExtras().getInt("docObjIndex")
,docLiked));
}
private void checkUserLiked(){
if(!userSignedIn){
File liked = new File(getFilesDir() + "/likes");
if(liked.exists()){
try {
BufferedReader br = new BufferedReader(new InputStreamReader(openFileInput("likes")));
String line;
while((line = br.readLine()) != null)
{
int pos = 0;
for(int i = 0 ;i < line.length() ;i++){
if(line.substring(i ,i + 1).equals("-")){
pos = i;
break;
}
}
int docId = Integer.parseInt(Globals.searchedDocs.get(getIntent().getExtras().getInt("docObjIndex"))
.docId);
if(line.substring(0 ,pos).equals(String.valueOf(docId))){
docLiked = true;
break;
}
}
br.close();
}catch (FileNotFoundException e){
docLiked = false;
//Toast.makeText(this ,"خطأ فتح الملف" ,Toast.LENGTH_LONG).show();
}catch (IOException e){
docLiked = false;
//Toast.makeText(this ,"خطأ فتح الذاكرة" ,Toast.LENGTH_LONG).show();
}
}else {
docLiked = false;
//Toast.makeText(this ,"الملف غير موجود" ,Toast.LENGTH_LONG).show();
}
}
checkUserRated();
}
private void checkUserRated(){
if(!userSignedIn){
File liked = new File(getFilesDir() + "/rates");
if(liked.exists()){
try {
BufferedReader br = new BufferedReader(new InputStreamReader(openFileInput("rates")));
String line;
while((line = br.readLine()) != null)
{
int pos = 0;
for(int i = 0 ;i < line.length() ;i++){
if(line.substring(i ,i + 1).equals("-")){
pos = i;
break;
}
}
int docId = Integer.parseInt(Globals.searchedDocs.get(getIntent().getExtras().getInt("docObjIndex"))
.docId);
if(line.substring(0 ,pos).equals(String.valueOf(docId))){
rate = Float.parseFloat(line.substring(pos + 1 ,line.length()));
docRated = true;
break;
}
}
br.close();
}catch (FileNotFoundException e){
docRated = false;
//Toast.makeText(this ,"خطأ فتح الملف" ,Toast.LENGTH_LONG).show();
}catch (IOException e){
docRated = false;
//Toast.makeText(this ,"خطأ فتح الذاكرة" ,Toast.LENGTH_LONG).show();
}
}else {
docRated = false;
//Toast.makeText(this ,"الملف غير موجود" ,Toast.LENGTH_LONG).show();
}
}
}
private void setActivityTitle(){
LayoutInflater lI = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = lI.inflate(R.layout.activity_title ,null ,false);
TextView tv = v.findViewById(R.id.activityTitle);
tv.setText(title);
headerProgressBar = v.findViewById(R.id.titleProgressBar);
getSupportActionBar().setCustomView(v);
}
public void loadMemberInfo(TextView memberInfo ,String memberId){
innerMemberInfo = memberInfo;
new Thread(new GetMemberInfo (memberId)).start();
}
public void showMemberInfo(JSONArray info){
if(info.length() > 0){
String processedInfo = "<ul>";
try {
for(int i = 0 ;i < info.length() ;i++){
processedInfo += "<li>" + info.getJSONObject(i).getString("info") + "</li>";
}
processedInfo += "</ul>";
innerMemberInfo.setText(Html.fromHtml(processedInfo));
}catch (JSONException e){
}
}
}
public void hideInfoProgressBar(){
docInfoProgressBar.setVisibility(ProgressBar.INVISIBLE);
if(!userSignedIn){
new Thread(new RecordDocView(0 ,
Integer.parseInt(Globals.searchedDocs.get(getIntent().getExtras().getInt("docObjIndex"))
.docId))).start();
}
}
public void hideHeaderProgressBar(){
headerProgressBar.setVisibility(ProgressBar.INVISIBLE);
}
public void assignInfoPBar(ProgressBar bar){
docInfoProgressBar = bar;
}
public  void recordUserLike(){
if(!userSignedIn){
new Thread(new RecordUserLike(0 ,
Integer.parseInt(Globals.searchedDocs.get(getIntent().getExtras().getInt("docObjIndex"))
.docId))).start();
headerProgressBar.setVisibility(ProgressBar.VISIBLE);
}
}
public void recordUserUnlike(){
int likeId = -1;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(openFileInput("likes")));
String line;
while ((line = br.readLine()) != null) {
int pos = 0;
for(int i = 0 ;i < line.length() ;i++){
if(line.substring(i ,i + 1).equals("-")){
pos = i;
break;
}
}
int docId = Integer.parseInt(Globals.searchedDocs.get(getIntent().getExtras().getInt("docObjIndex"))
.docId);
if(line.substring(0 ,pos).equals(String.valueOf(docId))){
likeId = Integer.parseInt(line.substring(pos + 1 ,line.length()));
break;
}
}
}catch (IOException e){
}
new Thread(new RecordUserLike(likeId)).start();
headerProgressBar.setVisibility(ProgressBar.VISIBLE);
}
public void assignUserLikeButton(ImageView likeButton){
userLikeButton = likeButton;
}
public void processLiked(int likeId ,int docId){
if(!userSignedIn){
userLikeButton.setImageResource(R.drawable.heart_solid);
OutputStreamWriter oSW ;
if(!userSignedIn){
try {
oSW = new OutputStreamWriter(openFileOutput("likes" ,MODE_APPEND));
oSW.append( docId + "-" + likeId);
oSW.append("nr");
oSW.flush();
oSW.close();
}catch (FileNotFoundException e){
Toast.makeText(this ,"خطأ فتح الملف" ,Toast.LENGTH_LONG).show();
}catch (IOException e) {
Toast.makeText(this, "خطأ فتح الذاكرة", Toast.LENGTH_LONG).show();
}
}
}
}
public void processUnliked(int likeId){
if(!userSignedIn){
File inputFile = new File(getFilesDir() + "/likes");
File tempFile = new File(getFilesDir() +  "/likes_temp");
try {
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String lineToRemove = Globals.searchedDocs.get(getIntent().getExtras().getInt("docObjIndex")).docId
+ "-" + likeId;
String currentLine;
while((currentLine = reader.readLine()) != null) {
// trim newline when comparing with lineToRemove
String trimmedLine = currentLine.trim();
if(trimmedLine.equals(lineToRemove)) continue;
writer.write(currentLine + System.getProperty("line.separator"));
}
writer.close();
reader.close();
tempFile.renameTo(inputFile);
}catch (IOException e){
}
userLikeButton.setImageResource(R.drawable.heart);
}
}
public void recordRating(final float value){
RATING_MODE = true;
headerProgressBar.setVisibility(ProgressBar.VISIBLE);
LayoutInflater lI = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (!userSignedIn){
final AlertDialog alert = new AlertDialog.Builder(this ,R.style.MyDialogTheme).create();
if(!docRated){
View alertView = lI.inflate(R.layout.rate_doc_alert ,null ,false);
alertView.setBackgroundColor(Color.TRANSPARENT);
((TextView) alertView.findViewById(R.id.rateAlertMessage)).setText("هل تود اختيار التقييم : " +
value + " لهذه الصفحة");
((Button) alertView.findViewById(R.id.alertPositiveButton)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Thread(new RecordRating(Globals.searchedDocs.get(getIntent().getExtras().getInt("docObjIndex")).docId
,value ,0)).start();
alert.dismiss();
RATING_MODE = true;
}
});
((Button) alertView.findViewById(R.id.alertNegativeButton)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
alert.dismiss();
hideHeaderProgressBar();
docRatingBar.setRating(0);
RATING_MODE = false;
}
});
alert.setView(alertView);
alert.show();
}else{
View alertView = lI.inflate(R.layout.rate_doc_alert ,null ,false);
alertView.setBackgroundColor(Color.TRANSPARENT);
((TextView) alertView.findViewById(R.id.rateAlertMessage)).setText("لقد قمت بالفعل بتقييم هذه الصفحة");
((Button) alertView.findViewById(R.id.alertPositiveButton)).setEnabled(false);
((Button) alertView.findViewById(R.id.alertNegativeButton)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
alert.dismiss();
docRatingBar.setRating(rate);
RATING_MODE = false;
hideHeaderProgressBar();
}
});
alert.setView(alertView);
alert.setCancelable(false);
alert.show();
}
}
}
public void processRated(int docId , float rateVal){
OutputStreamWriter oSW ;
if(!userSignedIn){
try {
oSW = new OutputStreamWriter(openFileOutput("rates" ,MODE_APPEND));
oSW.append( docId + "-" + rateVal);
oSW.append("nr");
oSW.flush();
oSW.close();
docRated = true;
docRatingBar.setRating(rateVal);
RATING_MODE = false;
rate = rateVal;
}catch (FileNotFoundException e){
Toast.makeText(this ,"خطأ فتح الملف" ,Toast.LENGTH_LONG).show();
resetRating();
}catch (IOException e) {
Toast.makeText(this, "خطأ فتح الذاكرة", Toast.LENGTH_LONG).show();
resetRating();
}
}
}
public void assignRateBar(RatingBar bar){
docRatingBar = bar;
}
public void assignAvViewPager(ViewPager vP){
avPager = vP;
avPager.setAdapter(new AvailabilityPagerAdapter(getIntent().getExtras().getInt("docObjIndex") ,getSupportFragmentManager()));
}
public boolean isRatingModeOn(){
return RATING_MODE;
}
public void resetRating(){
docRatingBar.setRating(0);
RATING_MODE = false;
}
}

这是 XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layoutDirection="rtl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f5f5f5"
tools:context="eng.hasan.medico.MemberPage">
<ListView
android:id="@+id/memberList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="60dp"
android:dividerHeight="10dp"
android:background="#f5f5f5"/>
<TextView
android:id="@+id/memberCustomersRatings"
android:padding="10dp"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_height="50dp"
android:textAlignment="center"
android:gravity="center_vertical"
android:textSize="18dp"
android:text="تقييمات الزائرين"
android:background="@drawable/rounded_bottom_corners_bg"/>
</RelativeLayout>

适配器构造函数与从该活动调用它的方式不匹配:

AvailabilityPagerAdapter(int searchedDocIndex)

AvailabilityPagerAdapter(getIntent().getExtras().getInt("docObjIndex") ,getSupportFragmentManager())

如果没有适当的上下文,适配器就不知道在哪里显示内容

相关内容

最新更新