F
F
foonfyrick2020-11-12 08:38:54
OOP
foonfyrick, 2020-11-12 08:38:54

How to bind the call of view elements to one activity or fragment?

I created FirstFragment and SecondFragment, I added a button to each, in the code, in SecondFragment I can call a button belonging to FirstFragment, and do anything with it, but there will be an error when the application starts. How can I make it so that I can only access those view elements in the code that are written in the called layout in the fragment? That is, if I write in FirstFragment, then only fragment_first layout elements will be available.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Mercury13, 2016-07-27
@Mercury13

Private and protected is when the object hides that it is inherited from the student. From the outside, neither the fact of inheritance nor paternal fields is visible.

class Father {};
class Son : private Father {};

int main()
{
    Son son;
    Father& q = son;
}

error: 'Father' is an inaccessible base of 'Son'
Private and protected - rather "hacks" and it is not recommended to use them. Although, of course, they use it to simplify their lives. I see two uses: 1) a good base for a completely foreign class; 2) implementation of the interface, which is needed only internally.
Here is an example of the second. The FileWriter object implements the Callback interface for its internal needs.
#include <iostream>

class Callback {
public:
    virtual void act(int x) = 0;
};

void generateFibonacci(int n, Callback& callback) {
    int x1 = 0, x2 = 1;
    for (int i = 0; i < n; ++i) {
        callback.act(x2);
        int x3 = x1 + x2;
        x1 = x2;
        x2 = x3;
    }
}

class FileWriter : private Callback {
public:
    FileWriter(std::ostream& aOs, int aN) : os(aOs), n(aN) {}
    void run() { generateFibonacci(n, *this); }
private:
    std::ostream& os;
    const int n;
    void act(int x) override { os << x << std::endl; }
};
using namespace std;

int main()
{
    FileWriter wri(std::cerr, 10);
    wri.run();
}

And if real life, then the object can be both QDialog (dialog box) and QAbstractItemModel (data model for some table lying on this window). Although, I repeat, this is more of a hack.
PS In Delphi and Java, all inheritance is public, and if you need to secretly implement some kind of interface or use a convenient class, then only with a hidden field. Just like in the comments.
PPS An example of the first. Some Array2d is hidden from Array1d, because it has a completely different interface. Array1d has alloc(n) and operator()(i), Array2d has alloc(m, n) and operator()(i, j). And Array1d is not a bad thing to be managed by a block of memory with a length of m × n elements.

D
Denis Zagaevsky, 2016-07-27
@zagayevskiy

Everything is simple here. public means that everyone will know that student1 inherited from student, student's public methods will be visible to the outside.
protected means that only student1 and its descendants know it. private means that only student1 knows about this inheritance.

I
illuzor, 2020-11-12
@foonfyrick

Use ViewBinding

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question