如何调用从活动的方法来改变布局? [英] How to call method from activity to change layout?

查看:110
本文介绍了如何调用从活动的方法来改变布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能叫 onItemClick <内部 VideoLayout changeTitle()方法/ code>活动的事件?

我试图找出什么是更改列表项内容的最佳实践。

v.changeTitle((VideoModel)videoAdapter.getItem(位置).title伪); (我认为这是说的长到了我脑海的第一个解决方案)抛出一个未定义的方法的错误。

任何toughts?

我就不解释了code,因为我认为这是自我解释的:


活动:

 公共类MainActivity扩展BaseActivity {    公共无效setVideosList()
    {
        videoAdapter =新VideoAdapter(这一点,videoItems);
        listView.setAdapter(videoAdapter);
        listView.setOnItemClickListener(新android.widget.AdapterView.OnItemClickListener(){
                @覆盖
        公共无效onItemClick(适配器视图&LT;&GT; AV,视图V,INT位置,长ARG3){        }
    });
    新getVideosTask()执行();
}

}

video.xml(我只是粘贴它的beggining,这个布局是一个列表项里面发生的事情)

 &LT; com.company.project.VideoLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直
机器人:paddingBottom会=15dp
机器人:paddingTop =15dp
机器人:paddingLeft =15dp
机器人:paddingRight =15dp
机器人:descendantFocusability =blocksDescendants
机器人:ID =@ + ID /包装&GT;

VideoAdapter.java:

 公共类VideoAdapter延伸BaseAdapter {
@覆盖
公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup)
{
    VideoLayout图。
    如果(convertView == NULL){
        鉴于=(VideoLayout)LayoutInflater.from(上下文).inflate(R.layout.video,父母,假);
    }其他{
        鉴于=(VideoLayout)convertView;
    }    VideoModel项目=(VideoModel)的getItem(位置);
    如果(项目!= NULL){
        。查看prepare(项目);
    }    返回视图。
}
}

VideoModel.java:

 类VideoModel {公共字符串称号;
公共字符串videoUrl;
公共字符串图片网址;

}

VideoLayout.java:

 类VideoLayout扩展的LinearLayout {
公共上下文的背景下;公共MyImageView ImageView的;
公共MyVideoView videoView;
公众的TextView titleview的;公共VideoLayout(上下文的背景下){
    超级(上下文);
    this.context =背景;
}公共VideoLayout(上下文的背景下,ATTRS的AttributeSet){
    超(背景下,ATTRS);
}公共VideoLayout(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
    超(背景下,ATTRS,defStyle);
}保护无效onFinishInflate()
{
    super.onFinishInflate();
    ImageView的=(MyImageView)findViewById(R.id.placeholder);
    videoView =(MyVideoView)findViewById(R.id.video);
    titleview的=(的TextView)findViewById(R.id.title);
}公共无效changeTitle(字符串文本){
    titleView.setText(文本);
}
}


解决方案

解决了!

我已经简化它(根据@ thinear的答案):

  @覆盖
        公共无效onItemClick(适配器视图&LT;&GT; AV,视图V,INT位置,
                长ID){
            VideoLayout包装=(VideoLayout)v.findViewById(R.id.wrapper);
            wrapper.changeTitle(称号);
        }

How can I call the changeTitle() method of VideoLayout inside the onItemClick event of the activity?

I'm trying to figure out what's the best practice to change the content of a list item.

Doing v.changeTitle((VideoModel) videoAdapter.getItem(position).title); (which I think it's the the first solution that's come up to my mind) throws a "undefined method" error.

Any toughts?

I won't explain the code because I think it's self explanatory:


Activity:

public class MainActivity extends BaseActivity {

    public void setVideosList()
    {
        videoAdapter = new VideoAdapter(this, videoItems);
        listView.setAdapter(videoAdapter);
        listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
                @Override
        public void onItemClick(AdapterView<?> av, View v, int position, long arg3) {

        }
    });
    new getVideosTask().execute();
}

}

video.xml (I just pasted the beggining of it. This layout is what goes inside a list item)

<com.company.project.VideoLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="15dp"
android:paddingTop="15dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:descendantFocusability="blocksDescendants"
android:id="@+id/wrapper">

VideoAdapter.java:

public class VideoAdapter extends BaseAdapter{
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
    VideoLayout view;
    if (convertView == null) {
        view = (VideoLayout) LayoutInflater.from(context).inflate(R.layout.video, parent, false);
    }else{
        view = (VideoLayout) convertView;
    }

    VideoModel item = (VideoModel) getItem(position);
    if (item != null) {
        view.prepare(item);
    }

    return view;
}
}

VideoModel.java:

class VideoModel {

public String title;
public String videoUrl;
public String imageUrl;

}

VideoLayout.java:

class VideoLayout extends LinearLayout{
public Context context;

public MyImageView imageView;
public MyVideoView videoView;
public TextView titleView;

public VideoLayout(Context context) {
    super(context);
    this.context = context;
}

public VideoLayout (Context context, AttributeSet attrs) {
    super(context, attrs);
}

public VideoLayout (Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

protected void onFinishInflate()
{
    super.onFinishInflate();
    imageView = (MyImageView) findViewById(R.id.placeholder);
    videoView = (MyVideoView) findViewById(R.id.video);
    titleView = (TextView) findViewById(R.id.title);
}

public void changeTitle(String text){
    titleView.setText(text);
}   
}

解决方案

Solved!

I've simplified it (based on @thinear's answer):

@Override
        public void onItemClick(AdapterView<?> av, View v, int position,
                long id) {
            VideoLayout wrapper = (VideoLayout) v.findViewById(R.id.wrapper);
            wrapper.changeTitle("title");
        }

这篇关于如何调用从活动的方法来改变布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆