Answer the question
In order to leave comments, you need to log in
Why are Fragments not converted?
Hello, friends. I'm learning app development for Android, started to study the fragments.
Essence: there is a main activity and two fragments on it. Task: to use FragmentManager for communication of fragments among themselves.
Activation code:
package com.example.myfragment;
import android.app.Activity;
import android.app.FragmentManager;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.support.v4.app.Fragment;
public class MainActivity extends Activity implements Fragment1.OnSelectedButtonListener{
@Override
public void onButtonSelected(int buttonIndex) {
// вызываем менеджер фрагментов
FragmentManager fragmentManager = getSupportFragmentManager;
// Получаем ссылку на второй фрагмент по ID
Fragment2 fragment2 = (Fragment2) fragmentManager.findFragmentById(R.id.fragment2);
// Выводим нужную информацию
if (fragment2 != null)
fragment2.setDescription(buttonIndex);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void Button1Click(View view){
Button btn = (Button)findViewById(R.id.button1);
TextView tv = (TextView)findViewById(R.id.textView1);
ImageView iv = (ImageView)findViewById(R.id.imageView1);
tv.setText(btn.getText());
//BitmapDrawable bd = getResources().getDrawable(R.drawable.paint1);
Drawable img = getResources().getDrawable(R.drawable.paint1);
iv.setImageDrawable(img);
}
public void Button2Click(View view){
Button btn = (Button)findViewById(R.id.button2);
TextView tv = (TextView)findViewById(R.id.textView1);
ImageView iv = (ImageView)findViewById(R.id.imageView1);
tv.setText(btn.getText());
//BitmapDrawable bd = getResources().getDrawable(R.drawable.paint1);
Drawable img = getResources().getDrawable(R.drawable.paint2);
iv.setImageDrawable(img);
}
public void Button3Click(View view){
Button btn = (Button)findViewById(R.id.button3);
TextView tv = (TextView)findViewById(R.id.textView1);
ImageView iv = (ImageView)findViewById(R.id.imageView1);
tv.setText(btn.getText());
//BitmapDrawable bd = getResources().getDrawable(R.drawable.paint1);
Drawable img = getResources().getDrawable(R.drawable.paint3);
iv.setImageDrawable(img);
}
}
package com.example.myfragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class Fragment1 extends Fragment implements View.OnClickListener{
public interface OnSelectedButtonListener{
void onButtonSelected(int buttonIndex);
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//return super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment1, container, false);
Button button1 = (Button) rootView.findViewById(R.id.button1);
Button button2 = (Button) rootView.findViewById(R.id.button2);
Button button3 = (Button) rootView.findViewById(R.id.button3);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
return rootView;
}
@Override
public void onClick(View v) {
// Обрабатываем нажатия на кнопки
int buttonIndex = translateToIndex(v.getId());
OnSelectedButtonListener listener = (OnSelectedButtonListener) getActivity();
listener.onButtonSelected(buttonIndex);
//Toast.makeText(getActivity(), String.valueOf(buttonIndex), Toast.LENGTH_LONG).show();
}
int translateToIndex(int id){
int index = -1;
switch(id){
case R.id.button1:
index = 1; break;
case R.id.button2:
index = 2; break;
case R.id.button3:
index = 3; break;
}
return index;
}
}
package com.example.myfragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class Fragment2 extends Fragment {
private TextView mInfoTextView;
private ImageView mCatImageView;
private String[] mCatDescriptionArray;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//return super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment2, container, false);
mInfoTextView = (TextView) rootView.findViewById(R.id.textView1);
mCatImageView = (ImageView) rootView.findViewById(R.id.imageView1);
mCatDescriptionArray = getResources().getStringArray(R.array.cats);
return rootView;
}
public void setDescription(int buttonIndex){
String catDescription = mCatDescriptionArray[buttonIndex];
mInfoTextView.setText(catDescription);
switch (buttonIndex){
case 1:
mCatImageView.setImageResource(R.drawable.paint1);
break;
case 2:
mCatImageView.setImageResource(R.drawable.paint2);
break;
case 3:
mCatImageView.setImageResource(R.drawable.paint3);
break;
default:
break;
}
}
}
Answer the question
In order to leave comments, you need to log in
Pay attention to the libraries used. If you are using the Support Library (to support older versions of Android), then the types must be from the appropriate libraries. For example:
From the first listing, you can see that the manager fragment is not included in the Support library, but the fragment is.
import android.app.Activity;
import android.app.FragmentManager;
...
import android.support.v4.app.Fragment;
docs.oracle.com/javase/tutorial
docs.oracle.com/javase/tutorial/java/IandI/subclas... section on casting.
Until you understand the language at least at the core level, it’s better not to go into android.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question