Android OnClickListener不工作在片段



我有一个片段,显示我从firebase拉下的用户详细信息。我试图设置一个OnClickListener所以登录用户可以改变他们的形象,但我似乎不能得到它的工作。我已经查看了日志,没有发现错误。我看过别人做类似的事情,但似乎找不出为什么我的不起作用。

编辑:我发现了问题,但现在有一个不同的问题,当片段第一次加载的Onclick不工作,直到我刷新的片段,然后Onclick工作,但我不知道为什么片段需要刷新的Onclick工作?

片段;

 ImageView adminImage;
  adminImage = v.findViewById(R.id.AdminProfilePicture);
        adminImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(),"Long hold to change image",Toast.LENGTH_SHORT).show();
            }
        });

       adminImage.setOnLongClickListener(new View.OnLongClickListener() {
           @Override
           public boolean onLongClick(View view) {
               Intent openGallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(openGallery,1000);
               return false;
           }
       });

设置片段的Main Activity;

   navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.nav_Account_admin:
                        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                                new ProfileAdminFragment()).commit();
                        break;
                    case R.id.nav_Games:
                        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                                new GamesAdminFragment()).commit();
                        break;
                    case R.id.nav_Users:
                        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                                new AllUsersAdminFragment()).commit();
                        break;
                    case R.id.nav_Fixture:
                        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                                new FixturesAdminFragment()).commit();
                        break;
                    case R.id.nav_News:
                        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                                new NewsAdminFragment()).commit();
                        break;
                    case R.id.nav_Contact:
                        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                                new ContactAdminFragment()).commit();
                        break;
                    default:
                }
                draw.closeDrawer(GravityCompat.START);
                return true;
            }
        });
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, draw, toolbar,
                R.string.nav_app_bar_open_drawer_description, R.string.navigation_drawer_close);
        draw.addDrawerListener(toggle);
        toggle.syncState();
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new ProfileFragment()).commit();
            navigationView.setCheckedItem(R.id.nav_Account);
        }
    }
XML;

 <RelativeLayout
        android:id="@+id/rellay1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/grad"
        android:paddingBottom="20dp">
        <RelativeLayout
            android:id="@+id/imageViewMain"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dp"
            android:background="@drawable/circle_border">

            <ImageView
                android:id="@+id/AdminProfilePicture"
                android:layout_width="120dp"
                android:layout_height="120dp"
                android:layout_margin="9dp"
                android:adjustViewBounds="true"
                android:background="@drawable/circle"
                android:padding="3dp"
                android:scaleType="centerInside"
                android:src="@drawable/ic_user" />
        </RelativeLayout>

XML

完整片段代码

public class ProfileAdminFragment extends Fragment {
    FirebaseAuth fAuth;
    FirebaseFirestore fStore;
    TextView uName, uEmail, uPhone;
    FusedLocationProviderClient fusedLocationProviderClient;
    TextView userlocation;
    ImageView adminImage;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_profile_admin, container, false);
        fAuth = FirebaseAuth.getInstance();
        fStore = FirebaseFirestore.getInstance();

        userlocation = v.findViewById(R.id.tv_location);
        fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getContext());
        if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            //   getLocation();
        } else {
            ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 44);
        }

        uName = v.findViewById(R.id.profileFullName);
        uEmail = v.findViewById(R.id.profileEmail);
        uPhone = v.findViewById(R.id.profilePhone);
        DocumentReference docR = fStore.collection("Users").document(fAuth.getCurrentUser().getUid());
        docR.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                if (documentSnapshot.exists()) {
                    uName.setText(documentSnapshot.getString("FullName"));
                    uEmail.setText(documentSnapshot.getString("UserEmail"));
                    uPhone.setText(documentSnapshot.getString("PhoneNumber"));
                }
            }
        });
        getLocation();
        // profile image
        adminImage = v.findViewById(R.id.AdminProfilePicture);
        adminImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(), "Long hold to change image", Toast.LENGTH_SHORT).show();
            }
        });

        adminImage.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                Intent openGallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(openGallery, 1000);
                return false;
            }
        });

        return v;
    }

请尝试添加android:clickable="true"在ImageView。

<RelativeLayout
    android:id="@+id/rellay1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/grad"
    android:paddingBottom="20dp">
    <RelativeLayout
        android:id="@+id/imageViewMain"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:background="@drawable/circle_border">

        <ImageView
            android:id="@+id/AdminProfilePicture"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:layout_margin="9dp"
            android:adjustViewBounds="true"
            android:background="@drawable/circle"
            android:padding="3dp"
            android:scaleType="centerInside"
            android:src="@drawable/ic_user"
            android:clickable="true" />
    </RelativeLayout>

例如,如果你从活动a调用片段,然后:将片段中的v初始化为公共静态。然后从活动中调用setOnClickListener。

在你的活动中:

YourFragment.v.findViewById("AdminProfilePicture").setOnClickListener (...);

如果问题解决了请回复

SOLVED所以我发现我正在加载我的用户配置文件而不是我的管理配置文件所以在加载片段时存在冲突所以为了Onclick工作,我必须手动刷新片段

从这个;

new ProfileFragment()).commit();

,

new ProfileAdminFragment()).commit();
  if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new ProfileAdminFragment()).commit();
            navigationView.setCheckedItem(R.id.nav_Account_admin);
        }

最新更新